本文介绍了Matplotlib 直方图或 Seaborn distplots 的 bin 没有轮廓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

将 matplotlib.pyplot 导入为 plt将 seaborn 作为 sns 导入泰坦尼克号 = sns.load_dataset('泰坦尼克号')plt.hist(泰坦尼克号['票价'],bins=30)

产生如下图:

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

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

将 matplotlib 导入为 mplmpl.rcParams['lines.linewidth'] = 1mpl.rcParams['lines.color'] = 'black'mpl.rcParams['patch.linewidth'] = 1mpl.rcParams['patch.edgecolor'] = '黑色'mpl.rcParams['axes.linewidth'] = 1mpl.rcParams['axes.edgecolor'] = 'black'

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

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

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

解决方案

作为 matplotlib 2.0 更新的一部分,条形图的边是

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.

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

yielded the following graph:

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.

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:

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.

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.

解决方案

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

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_kws argument,

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

For matplotlib plots, you can directly use the edgecolor or ec argument.

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:

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()

这篇关于Matplotlib 直方图或 Seaborn distplots 的 bin 没有轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:16