python - Seaborn 直方图使列变白

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

在什么情况下 Seaborn 会使直方图列变白?我在 Jupyter 笔记本中使用 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始终绘制蓝色柱?

blue columns

blue and white

white

最佳答案

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

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

更细的线宽

# 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() 之后,因为这可能会覆盖它们。

关于python - Seaborn 直方图使列变白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59694604/

相关文章:

cmd 路径上的 Python

python - 从 s3 读取 .pptx 文件

python - pip 或 pip3 为 Python 3 安装包?

python - 属性错误: 'Options' object has no attribute 'get_all_related_objects'

python - python中闭环曲线内的面积

python - 如何在地理上过滤 PySpark 中的条目?

python - pandas groupby column 然后根据第三列创建另外两列

python-3.x - 如何从python3中的同级目录导入?

python - 动画等高线和散点图

python - cartopy + networkx : zorder is not working