我的values中有三个元素,我想用三个元素做并排的两个饼图,它为什么报错了,我的代码: fig, axes = plt.subplots(figsize=(10,10),ncols=2) ax1,ax2=axes.ravel() values=[sum(bad_shot_list),sum(empty_shot_list),sum(noise_total_list)] bad_shot_list_num=[] empty_shot_list_num=[] for i in bad_shot_list: if i >0: bad_shot_list_num.append(i) for j in empty_shot_list: if j >0: empty_shot_list_num.append(j) print(bad_shot_list_num) print(len(bad_shot_list_num)) labels=["坏炮","丢炮","船噪音"] plt.subplot(1,2,1) patches,texts=ax1.pie(values,labels=labels,autopct="%.12f%%",colors=["red","blue","green"])## plt.figure(figsize=(10,10),dpi=100)## plt.pie(values,labels=labels,autopct="%.12f%%",colors=["red","blue","green"]) plt.subplot(1,2,2) patches,texts=ax1.pie(values,labels=labels,autopct="%.12f%%",colors=["red","blue","green"]) plt.legend() plt.title("2023YC3D RMS level percent") plt.show()
时间: 2024-03-14 19:48:18 浏览: 72
看起来你的代码中有一个错误,即在第二个饼图中,你应该使用ax2而不是ax1。因此,你的代码应该是这样的:
```
fig, axes = plt.subplots(figsize=(10,10),ncols=2)
ax1,ax2=axes.ravel()
values=[sum(bad_shot_list),sum(empty_shot_list),sum(noise_total_list)]
bad_shot_list_num=[]
empty_shot_list_num=[]
for i in bad_shot_list:
if i >0:
bad_shot_list_num.append(i)
for j in empty_shot_list:
if j >0:
empty_shot_list_num.append(j)
print(bad_shot_list_num)
print(len(bad_shot_list_num))
labels=["坏炮","丢炮","船噪音"]
plt.subplot(1,2,1)
patches,texts=ax1.pie(values,labels=labels,autopct="%.12f%%",colors=["red","blue","green"])##
plt.subplot(1,2,2)
patches,texts=ax2.pie(values,labels=labels,autopct="%.12f%%",colors=["red","blue","green"])
plt.legend()
plt.title("2023YC3D RMS level percent")
plt.show()
```
这样你应该能够成功地绘制两个并排的饼图。
阅读全文