属性错误:'Line2D'对象没有属性'xlabel'。

3

以下是我的代码

运行在Windows 10 上

将下面的代码保存到一个文件中,即testflask.py

import pandas as pd
import matplotlib.pyplot as plt
from flask import Flask, send_file, make_response, render_template
#from flask_ngrok import run_with_ngrok
import io

app = Flask(__name__)
#run_with_ngrok(app) 

@app.route("/")
def home():
  dates = ['2010-11', '2011-12', '2012-13', '2013-14', '2014-15', '2015-16', '2016-17']
  steps = [9000, 9500.756, 9800.859, 10000.262, 9800.972, 10500.058, 11300.703]
  df= pd.DataFrame(
      {'date': dates, 'steps': steps})
  fig, axes = plt.subplots(nrows=1, ncols=1)
  major_xtick=[2,3,4,5,6,7]
  major_xticklabel=['2010-11','2011-12','2012-13','2013-14','2014-15','2015-16','2016-17']
  color=['blue','green','blue']
  ax=df.plot(ax=axes,figsize=(10,8),linewidth=2,marker='o',xlabel="Year" \
          ,ylabel="steps" \
          ,title="2dline plot" \
          ,xlim=(0,8),ylim=(2000,15000) \
          , color=color)
  ax.set_xticks(major_xtick)
  ax.set_xticklabels(major_xticklabel)
  ax.legend()
  bytes_image = io.BytesIO()
  plt.savefig(bytes_image, format='png')
  bytes_image.seek(0)
  bytes_obj = bytes_image #do_plot()
    
  # return render_template('create_plot.html', pfile='abc')
  # return "<h3>  {{ pfile }} is successfully created</h3>"
  return send_file(bytes_obj,
                     attachment_filename='plot.png',
                     mimetype='image/png')

#if __name__ == '__main__':
    # app.run(debug=False)
#    app.run()

打开Anaconda命令提示符并切换到确切的位置,然后执行以下命令:

set FALSK_APP =testflask.py

然后运行flask命令

flask run

如果我们使用这个本地链接,会出现错误:AttributeError: 'Line2D' object has no property 'xlabel'

这段代码在使用 flask_ngrok 的 Google Colab 上完美运行,所以我不确定我在本地漏掉了什么。唯一的区别是在 Google Colab 上,我把app.run指令放在了__main__内部,而在我的本地环境中,我是外部运行的。鉴于这种情况,我希望保持外部运行。

我的本地环境版本详细信息如下:

Flask==1.1.2
Python 3.6.8 :: Anaconda, Inc.

无法找到确切原因,但是通过删除“xlabel”和“ylabel”,然后使用“ax.set_xlabel”和“ax.set_ylabel”,我能够解决问题。 - sakeesh
1个回答

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接