gpt4 book ai didi

python - 保存图形时 Matplotlib 未正确显示直方图

转载 作者:行者123 更新时间:2023-12-05 07:03:31 24 4
gpt4 key购买 nike

使用 matplotlib,我在一个图中绘制了 2 个直方图。目标是稍后将它们添加到 Latex 文档中。我对两者之间的区别感兴趣,所以我使用低透明度并将它们绘制在彼此之上。在 Spyder 中,当我内联绘图时,图像看起来很好。参见 wanted plot

wanted

当我使用 plt.savefig() 将图像导出为 PNG 时,图像看起来像这样。但是,这在 Latex 文档中效果不佳,因为缩放会被破坏。当我尝试将其导出为 PDF 文件时,直方图的条形似乎重叠,使其看起来有边缘,如 ugly plot 中所示。 .

ugly

我认为问题的原因是由于矢量格式,当放大和缩小 PDF 时,重叠发生变化。当完全放大时,它看起来与 PNG 相同,当缩小时重叠变得更大。如果有人知道解决方案,我将不胜感激。

我已经尝试过的事情:

  • 改变线宽/边色
  • 更改 matplotlibrc 文件
  • 使用 rwidth 改变 bin 的距离

我正在使用的代码:

    binwidth = (np.max(prediction) - np.min(prediction)) / (2*I**(1/3))
kwargs = dict(alpha=0.5, bins=np.arange(min(prediction), max(prediction) + binwidth, binwidth))

fig_path = '***.pdf'

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.hist(prediction.flatten(), **kwargs, label = 'NN')
ax.hist(hedging_error, **kwargs, label = 'BS')
ax.set_xlim((-3,3))
ax.set_xlabel('Hedging error')
ax.set_ylabel('Count')
ax.legend()
fig.savefig(fig_path)

最佳答案

要删除 bin 边缘,请使用 plt.hist(..., histt​​ype='stepfilled')

https://matplotlib.org/stable/gallery/statistics/histogram_histtypes.html

关于python - 保存图形时 Matplotlib 未正确显示直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63180436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com