pyplot.savefig 导出为空

pyplot.savefig with empty export

我正在使用 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()

这是 运行 您的代码没有该行

的结果