Python Matplotlib 直方图或 Seaborn distplots 的 bin 没有轮廓

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43080259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:29:37  来源:igfitidea点击:

No outlines on bins of Matplotlib histograms or Seaborn distplots

pythonpython-3.xmatplotlibseaborn

提问by Colin Lindley

While doing some practice problems using seaborn and a Jupyter notebook, I realized that the distplot() graphs did not have the darker outlines on the individual bins that all of the sample graphs in the documentation have. I tried creating the graphs using Pycharm and noticed the same thing. Thinking it was a seaborn problem, I tried some hist() charts using matplotlib, only to get the same results.

在使用 seaborn 和 Jupyter notebook 做一些练习题时,我意识到 distplot() 图形在文档中的所有示例图形所具有的各个 bin 上没有较暗的轮廓。我尝试使用 Pycharm 创建图表并注意到同样的事情。我认为这是一个seaborn 问题,我尝试了一些使用 matplotlib 的 hist() 图表,但得到了相同的结果。

import matplotlib.pyplot as plt
import seaborn as sns
titanic = sns.load_dataset('titanic')
plt.hist(titanic['fare'], bins=30)

yielded the following graph:

产生了以下图表:

enter image description here

在此处输入图片说明

Finally I stumbled across the 'edgecolor' parameter on the plt.hist() function, and setting it to black did the trick. Unfortunately I haven't found a similar parameter to use on the seaborn distplot() function, so I am still unable to get a chart that looks like it should.

最后,我偶然发现了 plt.hist() 函数上的 'edgecolor' 参数,并将其设置为黑色即可。不幸的是,我没有找到类似的参数用于 seaborn distplot() 函数,所以我仍然无法获得看起来应该的图表。

I looked into changing the rcParams in matplotlib, but I have no experience with that and the following script I ran seemed to do nothing:

我研究过在 matplotlib 中更改 rcParams,但我没有这方面的经验,我运行的以下脚本似乎什么也没做:

import matplotlib as mpl

mpl.rcParams['lines.linewidth'] = 1
mpl.rcParams['lines.color'] = 'black'
mpl.rcParams['patch.linewidth'] = 1
mpl.rcParams['patch.edgecolor'] = 'black'
mpl.rcParams['axes.linewidth'] = 1
mpl.rcParams['axes.edgecolor'] = 'black'

I was just kind of guessing at the value I was supposed to change, but running my graphs again showed no changes.

我只是在猜测我应该改变的值,但再次运行我的图表显示没有变化。

I then attempted to go back to the default settings using mpl.rcdefaults() but once again, no change.

然后我尝试使用 mpl.rcdefaults() 返回默认设置,但再次没有改变。

I reinstalled matplotlib using conda but still the graphs look the same. I am running out of ideas on how to change the default edge color for these charts. I am running the latest versions of Python, matplotlib, and seaborn using the Conda build.

我使用 conda 重新安装了 matplotlib,但图形看起来仍然相同。我对如何更改这些图表的默认边缘颜色的想法不多了。我正在使用 Conda 构建运行最新版本的 Python、matplotlib 和 seaborn。

回答by ImportanceOfBeingErnest

As part of the update to matplotlib 2.0 the edges on bar plots are turned off by default. However, you may use the rcParam

作为 matplotlib 2.0 更新的一部分,条形图上的边在默认情况下关闭的。但是,您可以使用 rcParam

plt.rcParams["patch.force_edgecolor"] = True

to turn the edges on globally.

全局打开边缘。

Probably the easiest option is to specifically set the edgecolor when creating a seaborn plot, using the hist_kwsargument,

可能最简单的选择是在创建 seaborn 绘图时使用hist_kws参数专门设置边缘颜色,

ax = sns.distplot(x, hist_kws=dict(edgecolor="k", linewidth=2))

For matplotlib plots, you can directly use the edgecoloror ecargument.

对于 matplotlib 图,您可以直接使用edgecolororec参数。

plt.bar(x,y, edgecolor="k")
plt.hist(x, edgecolor="k")

Equally, for pandas plots,

同样,对于熊猫图,

df.plot(kind='hist',edgecolor="k")

A complete seaborn example:

一个完整的seaborn示例:

import  numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

x = np.random.randn(100)
ax = sns.distplot(x, hist_kws=dict(edgecolor="k", linewidth=2))
plt.show()

enter image description here

在此处输入图片说明