gpt4 book ai didi

python - Matplotlib 保存为 pdf 未在条形图中显示阴影线 - 潜在错误?

转载 作者:行者123 更新时间:2023-12-04 11:45:37 30 4
gpt4 key购买 nike

我在 ubuntu 18.04 机器上使用 matplotlib 版本 3.1.1,并在 python 3.6.8 和 3.7.4 中在 2 个不同的机器上尝试了这段代码。

问题是当有超过 10 个项目时,颜色会自我重复,我做了一个快速更改以制作阴影线来区分。虽然这在显示中效果很好,但当我 savefig("*.pdf") 时,阴影不显示。 savefig("*.png") 似乎工作正常,如下图所示。

我在多个地方搜索过这个问题,但似乎找不到有效的解决方案。我也试过 PdfPages。

下面附上简单的代码和输出。我很感激任何建议。

代码:

import matplotlib
import matplotlib.pyplot as plt
import pandas as pd

matplotlib.__version__ #3.1.1
df=pd.DataFrame({'A':[0.1]*12, 'B':[0.1]*12}).T
tit='title'

ax=df.plot.bar(figsize=(11,8.5),stacked=True,title=tit)
nrows=len(df)
bars = ax.patches
hatches=['','-', '+', 'x','/','//','O','o','\\','\\\\']*nrows*10 #times rows*colors
#
hatches.sort()
hatches=hatches[0:len(bars)]
i=0
for bar in bars:# goes down a column with same color, then next with diff color and so on.
bar.set_hatch(hatches[i])
#bar.text('1')
#print(bar)
i+=1
ax.legend(#loc='lower center', loc='upper center',bbox_to_anchor=(0.5, -.05,0,0),fancybox=True, shadow=True, ncol=5,fontsize=5.5)

#plt.savefig('barhatch.png')

plt.savefig('barhatch.pdf')

实际剧情截图:

screenshot of actual plot

savefig("out.png"):

savefig("out.png")

pdf 的屏幕截图,因为我似乎无法附加:

screenshot of the pdf as I can't seem to attach

最佳答案

尝试使用不同的 facecolor对于填充的空间和 edgecolor对于舱口。
我想舱口在那里,但显示的颜色与下面的填充空间相同。我在 fill_between 遇到过类似的事情并在此 GitHub discussion 中找到了解决方案.

关于python - Matplotlib 保存为 pdf 未在条形图中显示阴影线 - 潜在错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59480695/

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