2016-05-29 10 views
0

私はこの2つのリンクを持つ "フラスコアプリ"を用意しています。例えば、localhost:5000/line_chartとlocalhost:5000/bar_chartのように、それぞれ異なるmatplotlib視覚化にマッピングします。フラスコアプリ内の異なるmatplotlibグラフへの複数のルートマッピング

私がサーバーを起動し、ルート(いずれか)をクリックすると、私は期待しているものを見ています。

はlocalhost:5000/bar_chart

enter image description here

私は戻って、他のリンクを表示すると、両方のグラフを破ります。

はlocalhost:5000/line_chart

enter image description here

はlocalhost: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

働いたこと。うわー..ありがとう。これは2週間過ぎて私を悩ませている。 –

関連する問題