gpt4 book ai didi

python - 堆叠条形图的边缘被切断

转载 作者:太空宇宙 更新时间:2023-11-03 20:18:46 24 4
gpt4 key购买 nike

我通过 Pandas 创建了一个堆积条形图,第一个和最后一个条形图位于图的边缘。他们被切断了。

我想要得到的图是一个堆积条形图和一个带有 2 y 轴的折线图。条形图层的值应显示在图层中。

我创建了一个 pd.DataFrame,其中包含以下内容:

       1.0  1.3  1.7  2.0   2.3   2.7   3.0  3.3  3.7   4.0   5.0
SS 15 2.0 0.0 1.0 1.0 4.0 8.0 5.0 6.0 6.0 10.0 19.0
SS 16 1.0 1.0 5.0 2.0 11.0 16.0 11.0 8.0 7.0 6.0 12.0
SS 17 1.0 5.0 3.0 3.0 5.0 6.0 5.0 9.0 8.0 9.0 13.0

每一行(SS 15-SS 17)应该是一个堆叠条形图,其中值是条形图的一层。折线图仅包含 3 个点,应用一条线连接。

我用来绘制的代码是:

fig, ax1 = plt.subplots()
ax1 = df.plot(kind='bar',
stacked=True,
figsize=(20,15),
fontsize=20,
grid=True,
rot=45,
align='center',
edgecolor='#000000',
colormap='Paired')
ax2 = ax1.twinx()
ax1.set_xlabel('X-LABEL', fontsize=25)
ax1.set_ylabel('1st Y-LABLE', fontsize=25)
ax1.set_title(name, fontsize=35)
ax1.legend(fontsize=15, loc='upper left')
ax1.grid(True, axis='y')
df2 = pd.DataFrame(medi) #medi is just a dict with 3 pais
df2.plot(ax=ax2,
color='red',
marker='o',
linewidth='2',
fontsize=20)
ax2.legend(["2nd LEGEND"], fontsize=15, loc='upper right')
ax2.set_ylim(1,5)
ax2.set_ylabel('2nd Y-LABEL', fontsize=25)
ax2.grid(True,axis='y')

这会导致:

enter image description here

正如您所看到的,第一个和最后一个条形位于图表的边缘并被切断。我尝试覆盖 xticks 但没有成功。

我遇到的第二个问题是显示栏中的值。我找到了解决方案here ,但会导致错误:

AttributeError: 'NoneType' object has no attribute 'update'

最佳答案

我找到了解决方案。

我通过matplotlib创建了堆叠条形图,并且效果完美。

我不知道为什么图表被 pandas 截断,但它有效。

关于python - 堆叠条形图的边缘被切断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58286543/

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