gpt4 book ai didi

python - pyplot.savefig 与空导出

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

我正在使用 pyplot.subplot() 制作条形图,然后另存为图像。目前 savefig 正在保存一个空图像。我想我有点卡住了,因为我没有找到很多关于条形图和子图的示例。

代码如下:

nums = [57, 83, 66, 90, 84, 83, 83, 64, 58, 94, 94, 79, 95, 61, 67, 79, 70, 86, 84, 57, 80, 98, 54, 82, 74, 87, 69, 55, 59, 87, 76, 53, 80, 89, 66, 68, 95, 89, 65, 69, 81, 55, 53, 77, 54, 88, 90, 73, 77, 63, 75, 98, 78, 62, 61, 91, 90, 69, 93, 80, 69, 63, 71, 57, 77, 76, 77, 67, 91, 66, 51, 98, 66, 96, 62, 98, 78, 86, 60, 93, 60, 62, 72, 71, 55, 55, 82, 99, 83, 61, 69, 58, 68, 71, 67, 87, 64, 50, 60]
gradeA = []
gradeB = []
gradeC = []
gradeD = []
gradeF = []

for a in nums:
if a in range(90,100):
gradeA.append(a)
if a in range(80,89):
gradeB.append(a)
if a in range(70,79):
gradeC.append(a)
if a in range(60,69):
gradeD.append(a)
if a in range(0,59):
gradeF.append(a)

import numpy as np
import matplotlib.pyplot as plt

gradeCounts = (len(gradeF), len(gradeD), len(gradeC), len(gradeB), len(gradeA))

# the x locations for the bars
ind = np.arange(5)
#bar width
width = 0.35

#draw graph
ax = plt.subplot()

#set up colors for different bars
colors = ['r','orange','y','g','b']
ax.bar(ind+.5*width, gradeCounts, width, color=colors)

# add some text for labels, title and axes ticks
ax.set_title('Grade Range')
ax.set_xticks(ind+width)
ax.set_xticklabels( ('F', 'D', 'C', 'B', 'A') )

plt.show()
plt.savefig('test.png')

最佳答案

去掉倒数第二行,plt.show()

这是在没有该行的情况下运行代码的结果 enter image description here

关于python - pyplot.savefig 与空导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29879072/

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