为什么我得到“AttributeError:'tuple'对象没有属性'savefig'?

2024-10-01 11:40:48 发布

您现在位置:Python中文网/ 问答频道 /正文


编辑2017年3月15日下午12:00 CDT:我已经设法修复了程序中的错误,并按设计完成了程序。我要感谢berna111和TigerhawkT3提交的答案,因为他们允许我完成这个项目。再次感谢,堆栈溢出!在


我试图将一系列数组构建的直方图(用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'

错误涉及的部分如下:

^{pr2}$

为什么我会收到这个错误,我如何修复它?如果有人能告诉我,我将不胜感激。在


注意事项:

  • 我在谷歌上做了一些搜索,发现了一些类似的错误,但是没有一个错误被解释为元组。我不知道元组部分是从哪里来的。

  • 直方图创建步骤中的“_graph_array”项是10长1高的维数组。总共10个项目,指定为浮动类型。

  • 保存步骤中的“filename”变量表示包含日期和时间的字符串。



Tags: 项目程序编辑类型png错误步骤数组
2条回答

documentation for ^{}

The return value is a tuple (n, bins, patches) or ([n0, n1, ...], bins, [patches0, patches1,...]) if the input contains multiple data.

documentation for ^{}

Save the current figure.

看起来您应该以调用savefig的方式调用hist,而不是调用{}的结果。在

plt.savefig("{}_temperature.png".format(filename), format='png')
...

我已经修改了您的代码,并在理解for循环的过程中,随意更改了几行,创建了一个按数字排列的列表:

import matplotlib.pyplot as plt
# should be equal when using .pylab
import numpy.random as rnd

# generate_data
n_points = 1000
temperature_graph_array = rnd.random(n_points)
feelslike_graph_array = rnd.random(n_points)
windspeed_graph_array = rnd.random(n_points)
windgustspeed_graph_array = rnd.random(n_points)
pressure_graph_array = rnd.random(n_points)
humidity_graph_array = rnd.random(n_points)
list_of_data = [temperature_graph_array,
                feelslike_graph_array,
                windspeed_graph_array,
                windgustspeed_graph_array,
                pressure_graph_array,
                humidity_graph_array]
list_of_names = ['temperature',
                 'feelslike',
                 'windspeed',
                 'windgustspeed',
                 'pressure',
                 'humidity']

# create the figures:
#figure1 = plt.figure()
#figure2 = plt.figure()
#figure3 = plt.figure()
#figure4 = plt.figure()
#figure5 = plt.figure()
#figure6 = plt.figure()
#list_of_figs = [figure1, figure2, figure3, figure4, figure5, figure6]
## could be:
list_of_figs = [plt.figure() for i in range(6)]

# create the axis:
#ax1 = figure1.add_subplot(111)
#ax2 = figure2.add_subplot(111)
#ax3 = figure3.add_subplot(111)
#ax4 = figure4.add_subplot(111)
#ax5 = figure5.add_subplot(111)
#ax6 = figure6.add_subplot(111)
#list_of_axis = [ax1, ax2, ax3, ax4, ax5, ax6]
## could be:
list_of_axis = [fig.add_subplot(111) for fig in list_of_figs]

# plot the histograms
# notice `plt.hist` returns a tuple (n, bins, patches) or
# ([n0, n1, ...], bins, [patches0, patches1,...]) if the input
# contains multiple data
#hist1 = ax1.hist(temperature_graph_array, color="blue")
#hist2 = ax2.hist(feelslike_graph_array, color="blue")
#hist3 = ax3.hist(windspeed_graph_array, color="blue")
#hist4 = ax4.hist(windgustspeed_graph_array, color="blue")
#hist5 = ax5.hist(pressure_graph_array, color="blue")
#hist6 = ax6.hist(humidity_graph_array, color="blue")
#list_of_hists = [hist1, hist2, hist3, hist4, hist5, hist6]
## could be:
list_of_hists = []
for i, ax in enumerate(list_of_axis):
    list_of_hists.append(ax.hist(list_of_data[i], color="blue"))

filename = 'output_graph'
for i, fig in enumerate(list_of_figs):
    name = list_of_names[i].capitalize()
    list_of_axis[i].set_title(name)
    fig.tight_layout()
    fig.savefig("{}_{}.png".format(filename,name), format='png')

不会张贴结果图,但这给了我6个小.png文件在同一个文件夹脚本。在

更好的是,您可以使用函数对数据执行所有操作:

^{pr2}$

相关问题 更多 >