2016-05-29 100 views
0

我有两个链接,每个映射到不同的matplotlib可视化文件,例如:localhost:5000/line_chart和localhost:5000/bar_chart。在烧瓶中映射到不同matplotlib图形的多路径应用程序

当我启动服务器,并点击一条路径(其中任何一个),我看到了我的期望。

本地主机:5000/bar_chart

enter image description here

当我回去查看其他环节,两个图形突破。

本地主机:5000/line_chart

enter image description here

本地主机:5000/bar_chart

enter image description here

我可以通过关闭服务器,然后运行 ​​“run.py” 重现此每次脚本再次。似乎是与内存缓冲区的覆盖冲突。有没有人有过这个问题?

应用程序/ views.py

import matplotlib 
matplotlib.use('Agg') # this allows PNG plotting 
import matplotlib.pyplot as plt 
import base64 

from flask import render_template 
from app import app 
from io import BytesIO 

@app.route('/') 
@app.route('/index') 
def index(): 
res = '' 
navigation = [['Line Chart','line_chart'],['Bar Chart','bar_chart']] 
res = res + '<h1>Matplotlib Chart Examples</h1>' 
res = res + '<ul>' 

for item in navigation: 
    name = item[0] 
    link = item[1] 
    res = res + '<li><a href="' + link + '">'+ name +'</a></li>' 

res = res +'</ul>' 
return res 


@app.route('/bar_chart') 
    def bar_chart(): 

    movies = ["Annie Hall", "Ben-Hur", "Casablanca", "Gandhi", "West Side Story"] 
    num_oscars = [5, 11, 3, 8, 10] 

    # bars are by default width 0.8, so we'll add 0.1 to the left coordinates 
    # so that each bar is centered 

    xs = [i + 0.1 for i, _ in enumerate(movies)] 

    # plot bars with left x-coordinates [xs], heights [num_oscars] 
    plt.bar(xs, num_oscars) 
    plt.ylabel("# of Academy Awards") 
    plt.title("My Favorite Movies") 

    # label x-axis with movie names at bar centers 
    plt.xticks([i + 0.5 for i, _ in enumerate(movies)], movies) 

    return compute(plt) 


@app.route('/line_chart') 
def line_chart(): 

    years = [1950, 1960, 1970, 1980, 1990, 2000, 2010] 
    gdp = [300.2, 543.3, 1075.9, 2862.5, 5979.6, 10289.7, 14958.3] 

    # create a line chart, years on x-axis, gdp on y-axis 
    plt.plot(years, gdp, color='green', marker='o', linestyle='solid') 

    # add a title 
    plt.title("Nominal GDP") 

    # add a label to the y-axis 
    plt.ylabel("Billions of $") 

    return compute(plt) 


def compute(plt): 
    # run plt.plot, plt.title, etc. 
    figfile = BytesIO() 
    plt.savefig(figfile, format='png') 
    figfile.seek(0) # rewind to beginning of file 

    #figfile.getvalue() extracts string (stream of bytes) 
    figdata_png = base64.b64encode(figfile.getvalue()) 

    return render_template('index.html', 
         title='matplotlib chart', 
         results=figdata_png) 

谢谢您的时间。

回答

1

我猜你需要两个数字,测试此代码,并告诉发生了什么:

@app.route('/bar_chart') 
    def bar_chart(): 

    movies = ["Annie Hall", "Ben-Hur", "Casablanca", "Gandhi", "West Side Story"] 
    num_oscars = [5, 11, 3, 8, 10] 

    # bars are by default width 0.8, so we'll add 0.1 to the left coordinates 
    # so that each bar is centered 

    xs = [i + 0.1 for i, _ in enumerate(movies)] 

    # plot bars with left x-coordinates [xs], heights [num_oscars] 
    plt.figure(1) 
    plt.bar(xs, num_oscars) 
    plt.ylabel("# of Academy Awards") 
    plt.title("My Favorite Movies") 

    # label x-axis with movie names at bar centers 
    plt.xticks([i + 0.5 for i, _ in enumerate(movies)], movies) 

    return compute(plt, 1) 


@app.route('/line_chart') 
def line_chart(): 

    years = [1950, 1960, 1970, 1980, 1990, 2000, 2010] 
    gdp = [300.2, 543.3, 1075.9, 2862.5, 5979.6, 10289.7, 14958.3] 

    # create a line chart, years on x-axis, gdp on y-axis 
    plt.figure(2) 
    plt.plot(years, gdp, color='green', marker='o', linestyle='solid') 

    # add a title 
    plt.title("Nominal GDP") 

    # add a label to the y-axis 
    plt.ylabel("Billions of $") 

    return compute(plt,2) 

def compute(plt, fignum): 
    # run plt.plot, plt.title, etc. 
    plt.figure(fignum) 
    figfile = BytesIO() 
    plt.savefig(figfile, format='png') 
    figfile.seek(0) # rewind to beginning of file 

    #figfile.getvalue() extracts string (stream of bytes) 
    figdata_png = base64.b64encode(figfile.getvalue()) 

    return render_template('index.html', 
         title='matplotlib chart', 
         results=figdata_png) 
+0

奏效。哇..非常感谢你。在过去的两周里,这一直困扰着我。 –

相关问题