2017-11-17 134 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