1
我必須在燒瓶模板中顯示2個matplotlib圖。我正在得到所需的輸出,但是,因爲我是matplotlib的新手,我無法弄清楚爲什麼刷新後第一個棒圖更改爲第二個圖。這裏有三個功能正在完成這項工作。燒瓶模板matplotlib圖刷新後錯誤
import matplotlib.pyplot as plt
import base64
import StringIO
@app.route('/FirmIn/admin/')
def admin():
plot_url=rendergraph1()
plot_url2=rendergraph2()
return render_template('admin.html', plot_url=plot_url, plot_url2=plot_url2)
def rendergraph1():
cursor.execute("SELECT category,count(*) from project_info group by category")
dataall=cursor.fetchall()
img = StringIO.StringIO()
category,index,count =[],[],[]
i=0
for data in dataall:
category.append(data[0])
index.append(i)
count.append(data[1])
i=i+1
print index, count
plt.bar(index, count, color = 'r')
plt.xticks(index, category, rotation=25)
plt.yticks(range(min(count), max(count)+1))
plt.rcParams['xtick.major.pad']='5'
plt.savefig(img, format='png')
img.seek(0)
plot_url = base64.b64encode(img.getvalue()).decode()
return plot_url
def rendergraph2():
cursor.execute("SELECT category,sum(project_cost) from project_info group by category")
img = StringIO.StringIO()
dataall=cursor.fetchall()
category,index,cost =[],[],[]
i=0
for data in dataall:
category.append(data[0])
index.append(i)
cost.append(data[1])
i=i+1
plt.plot(index,cost)
plt.xticks(index, category, rotation=25)
plt.savefig(img, format='png')
img.seek(0)
plot_url2 = base64.b64encode(img.getvalue()).decode()
return plot_url2
而且裏面的模板,我使用顯示:
<img style="height:400px" src="data:image/png;base64, {{ plot_url }}">
<img style="height:400px" src="data:image/png;base64, {{ plot_url2 }}">