Matplotlib直方图或Seaborn变形图的框上没有轮廓 [英] No outlines on bins of Matplotlib histograms or Seaborn distplots

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

问题描述

在使用seaborn和Jupyter笔记本进行一些练习问题时,我意识到distplot()图在文档中所有样本图所具有的单个bin上没有较暗的轮廓.我尝试使用Pycharm创建图形并注意到了同样的事情.认为这是一个严重的问题,所以我使用matplotlib尝试了一些hist()图表,但结果却完全相同.

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)

产生下图:

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

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.

我研究过在matplotlib中更改rcParams,但是我对此没有经验,我运行的以下脚本似乎无济于事:

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.

然后我尝试使用mpl.rcdefaults()返回默认设置. 但还是没有变化.

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

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

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.

推荐答案

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

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

全局打开边缘.

最简单的选择也许是使用hist_kws自变量

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

对于matplotlib图,您可以直接使用edgecolorec参数.

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

完整的示例:

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变形图的框上没有轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆