【发布时间】:2017-03-14 22:00:18
【问题描述】:
编辑 2017 年 3 月 15 日下午 12:00 CDT: 我已设法修复程序中的错误并按设计完成程序。我要感谢 berna1111 和 TigerhawkT3 提交的答案,因为他们让我完成了这个程序。再次感谢 Stack Overflow!
我正在尝试将一系列数组构建的直方图(使用 numpy 和使用 matplotlib 的直方图制作的数组)保存到 .png 类型的文件中。我收到以下错误消息:
Traceback (most recent call last):
File "C:/Users/Ryan/PycharmProjects/NWS/weather_data.py", line 475, in <module>
figure1.savefig("{}_temperature.png".format(filename))
AttributeError: 'tuple' object has no attribute 'savefig'
错误所指的部分如下:
figure1 = plt.hist(temperature_graph_array, color="blue")
figure2 = plt.hist(feelslike_graph_array, color="blue")
figure3 = plt.hist(windspeed_graph_array, color="blue")
figure4 = plt.hist(windgustspeed_graph_array, color="blue")
figure5 = plt.hist(pressure_graph_array, color="blue")
figure6 = plt.hist(humidity_graph_array, color="blue")
figure1.savefig("{}_temperature.png".format(filename), format='png')
figure2.savefig("{}_feelslike.png".format(filename), format='png')
figure3.savefig("{}_windspeed.png".format(filename), format='png')
figure4.savefig("{}_windgustspeed.png".format(filename), format='png')
figure5.savefig("{}_pressure.png".format(filename), format='png')
figure6.savefig("{}_humidity.png".format(filename), format='png')
为什么我会收到此错误,我该如何解决?如果有人能告诉我,我将不胜感激。
注意事项:
我进行了一些谷歌搜索,发现了一些类似的错误,但没有一个将数字解释为元组。我不明白元组部分来自哪里。
直方图创建步骤中的“_graph_array”项是维度为 10 长、1 高的数组。里面总共 10 个项目,指定为 Float 类型。
保存步骤中的“filename”变量代表一个包含日期和时间的字符串。
【问题讨论】:
-
plt.hist不返回figure实例,您应该创建一个图形(fig1 = plt.figure()),在其上创建一个轴(ax1 = fig1.add_subplots(111)),然后在轴上绘制(ax1.hist(...))。此时您应该能够保存该图 (fig1.savefig(...))。测试后会发布答案。 -
更正:
ax1 = fig1.add_subplot(111),而不是ax1 = fig1.add_subplot*s*(111)!
标签: python arrays numpy matplotlib attributeerror