2017-03-14 313 views
0

編集2017年3月15日12:00 PM CDT:私はプログラムでエラーを修正し、それを設計したとして、プログラムを完了するために管理しています。彼らが私にこのプログラムを完了させるのを許してくれたので、berna1111とTigerhawkT3に答えてくれたことに感謝したいと思います。もう一度、スタックオーバーフローありがとう!なぜ "AttributeError: 'tuple'オブジェクトに属性 'savefig'がありませんか?


私はタイプ.pngファイルするために、アレイ内蔵ヒストグラム(matplotlibのを使用してnumpyのヒストグラムで作られた配列)のシリーズを保存しようとしています。私は、次のエラーメッセージを受信して​​います:

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') 

は、なぜ私はこのエラーが発生します、そしてどのように私はそれを修正することができますか?誰かが私に教えてくれたら、私はそれを大いに感謝するでしょう。


注:

  • 私はいくつかのGoogle検索を行って、いくつかの同様のエラーが、数字は、タプルとして解釈されたなしを発見しました。タプルの部分がどこから来ているのか分かりません。

  • ヒストグラム作成ステップの「_グラフ配列」アイテムは、長さ10の長さが1の高さの配列です。内部に合計10個のアイテムがあり、フロートタイプとして指定されています。

  • 保存ステップの "filename"変数は、日付と時刻を含む文字列を表します。

    documentation for matplotlib.pyplot.histから

+0

plt.hist'は 'figure' instaceを返しません'、あなたは数字( 'FIG1 = PLTを作成する必要があります。( 'ax1 = fig1.add_subplots(111)')、次に軸( 'ax1.hist(...)')を描画します。その時点で、Figureを保存することができます( 'fig1.savefig(...)')。テスト後に回答を投稿します。 – berna1111

+0

修正: 'ax1 = fig1.add_subplot' * s *'(111) 'ではなく、' ax1 = fig1.add_subplot(111) '! – berna1111

答えて

2

私はあなたのコードを適応し、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ファイルがあります。

さらに良いことに、あなたはあなたのデータにすべてのことを行うための機能を使用することができます。

def save_hist(data, name, filename): 
    fig = plt.figure() 
    ax = fig.add_subplot(111) 
    ax.hist(data, color="blue") 
    ax.set_title(name) 
    fig.tight_layout() 
    fig.savefig("{}_{}.png".format(filename,name), format='png') 
    plt.close(fig) 

filename = 'output_graph_2' 
for data, name in zip(list_of_data, list_of_names): 
    save_hist(data, name, filename) 
+0

よろしくお願いします、ありがとうございます! –

3

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

documentation for matplotlib.pyplot.savefigから:

Save the current figure.

あなたがいない上、あなたはhist呼び出すのと同じ方法でsavefigを呼び出す必要がありますように見えますhist呼び出しの結果

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

あなたの答えをありがとう、しかしこれは動作しません。 "hist"関数を実行すると、実際にグラフが作成されます。グラフを作成しなければ、保存するグラフはありません。あなたのソリューションは、単にグラフの作成を削除します。 –

+0

@RyanWitek - はい、あなたはHIST( 'を呼び出す必要があります)' '、その後savefig()'が、 'savefig'はmatplotlib.pyplot''の方法ではなく、どのような 'HIST()'を返します(Aの方法でありますタプル)。 – TigerhawkT3

関連する問題