2017-11-17 3 views
0

私は非常に新しいプログラマーです。棒グラフをpngに保存すると問題が発生し、棒が表示されません。グラフをpngに保存するときにバーが表示されない

マイコード:

import numpy as np 
import matplotlib.pyplot as plt 

N = 3 
ind = np.arange(N) 
width = 0.35 

Start_means = (100, 50, 50) 
Start_std = (2, 3, 4) 

End_means = (80, 30, 30) 
End_std = (3, 5, 2) 

fig, ax = plt.subplots() 
rects1 = ax.bar(ind, Start_means, width, color='xkcd:red', yerr=Start_std) 
rects2 = ax.bar(ind+width, End_means, width, color='xkcd:black', yerr=End_std) 

ax.legend((rects1[0], rects2[0]), ('Start', 'End')) 
ax.set_ylabel('Available') 
ax.set_title('Travel availability, by tour') 
ax.set_xticks(ind + width/2) 
countries = ['Italy', 'China', 'France'] 
ids = ['ID:12345', 'ID:13579', 'ID:24680'] 

xlabels = [] 
for i, j in zip(countries, ids): 
    xlabels.append(i + '\n' + j) 

ax.set_xticklabels(xlabels) 

def autolabel(rects): 
    for rect in rects: 
     height = rect.get_height() 
     ax.text(rect.get_x() + rect.get_width()/2., 1.05*height, 
       '%d' % int(height), 
       ha='center', va='bottom') 
autolabel(rects1) 
autolabel(rects2) 

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

それがどのように見えるか:here 私はPNGファイルとして保存したいのですが、それはただの棒なしで空白アップします。

答えて

1

は、あなたは、単に順番を入れ替える必要があるものでplt.show()表示され、plt.savefig('barchart.png')

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

plt.savefigがショーを呼び出した後に動作しないことを理由 現在のフィギュアがリセットされたということです。

出典:https://stackoverflow.com/a/21884187/1577947

関連する問題