gpt4 book ai didi

python - Seaborn 显示一个条形图而不是多个图

转载 作者:太空宇宙 更新时间:2023-11-03 15:32:14 26 4
gpt4 key购买 nike

我的代码看起来像

ylst = [.1,.2,.3,.4,.5,.6,.7,.8]
xlst=["A","B","C","D","E","F","G","H"]
with PdfPages('probs.pdf') as pdf_pages:
for i in range(0,len(xlst), 2):
ax=sns.barplot(xlst[i:i+2], ylst[i:i+2])
ax.axes.set_title('Which name', fontsize=24,color="b",alpha=0.3)
ax.set_ylabel("Probability (%)",size = 17,color="r",alpha=0.5)

for p in ax.patches:
ax.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.005))
pdf_pages.savefig()

我想要 2 行 2 列矩阵中的 4 个图。也就是说,左上图应显示 AB,其值为 0.1 和 .2。然后,左下或右上图应显示值为 0.3 和 0.4 的 CD。等等

但是,我得到的是:

enter image description here

我该如何解决这个问题?

最佳答案

我已经测试过这有效:

import matplotlib.pyplot as plt, seaborn as sns
from matplotlib.backends.backend_pdf import PdfPages

ylst = [.1,.2,.3,.4,.5,.6,.7,.8]
xlst=["A","B","C","D","E","F","G","H"]
with PdfPages('probs.pdf') as pdf_pages:
fig,((ax1,ax2),(ax3,ax4)) = plt.subplots(2,2)
list_of_axes = (ax1,ax2,ax3,ax4)
for i,j in zip(range(0,len(xlst), 2),list_of_axes):
sns.barplot(xlst[i:i+2], ylst[i:i+2],ax=j)
j.axes.set_title('Which name', fontsize=24,color="b",alpha=0.3)
j.set_ylabel("Probability (%)",size = 17,color="r",alpha=0.5)

for p in j.patches:
j.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.005))
pdf_pages.savefig()
plt.close()

我刚刚为每个子图分配了一个轴,并将 .savefig() 移到了循环之外。

关于python - Seaborn 显示一个条形图而不是多个图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42822707/

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