【问题标题】:Seaborn histogram makes columns whiteSeaborn 直方图使列变白
【发布时间】:2020-01-11 12:57:55
【问题描述】:

在什么情况下 Seaborn 将直方图列设为白色?我在 Jupyter notebook 中使用 Seaborn:

import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

然后我使用这个函数绘制直方图:

def plot_hist(data, xlabel, bins=None):

  if not bins:
      bins = int(np.sqrt(len(data)))

  _= plt.xlabel(xlabel)
  _= plt.hist(data, bins=bins)

因此,在某些情况下,我的直方图包含所有蓝色列或一些蓝色和一些白色或只有白色列。请看附图。

如何让Seaborn总是画蓝柱?

【问题讨论】:

  • 通常白色会出现在黑色背景的主题中。也许您调用的某些功能会改变主题?尝试在调用histsns.set(palette='deep')之前将调色板显式设置为default palette

标签: python python-3.x matplotlib jupyter-notebook seaborn


【解决方案1】:

我认为问题在于直方图的edgecolorwhite,并且当您增加箱数或减少条形的宽度时,edgecolor 开始覆盖facecolor。你应该可以通过使用更高的 dpi 来修复它,

# globally
from matplotlib import rcParams
rcParams['figure.dpi'] = 300
# or for only this figure
fig = plt.figure(dpi=300)

更薄的linewidth

# globally
from matplotlib import rcParams
rcParams['patch.linewidth'] = 0.5
# or for only this plot
_= plt.hist(data, bins=bins, linewidth=0.5)

或完全删除轮廓,

_= plt.hist(data, bins=bins, edgecolor='none')

请注意,全局方法可能需要在 sns.set() 之后,因为这可能会覆盖它们。

【讨论】:

  • 谢谢!更薄的“线宽”只会在某种程度上使事情变得更好。条形仍然非常苍白,在某些情况下几乎是白色的。更高的 dpi 无济于事,它只会增加图像大小。删除轮廓完全没有效果。
  • @dokondr 你试过0.0的线宽吗?
  • 有趣的是,标准 matplotlib.pyplot 使用默认设置正确绘制所有内容。
  • @dokondr matplotlib 不再默认为直方图的轮廓,但 seaborn 为 sns.set() 中的样式添加了轮廓
  • @dokondr 文档对edgecolor 具有误导性,它应该是“无”而不是Noneplt.hist(data, bins=bins, edgecolor='none') 也可以(我已经相应地编辑了答案)
猜你喜欢
  • 2022-01-25
  • 2017-03-11
  • 2020-07-28
  • 2019-04-09
  • 2022-01-16
  • 1970-01-01
  • 2018-12-04
相关资源
最近更新 更多