Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为pyplot定义画布以便在HTML文档中打印?_Python_Html_Django_Matplotlib_Canvas - Fatal编程技术网

Python 如何为pyplot定义画布以便在HTML文档中打印?

Python 如何为pyplot定义画布以便在HTML文档中打印?,python,html,django,matplotlib,canvas,Python,Html,Django,Matplotlib,Canvas,我正在使用Django制作一个网页。我试图使使用matplotlib创建的条形图显示在我的网页上,但收到以下错误消息,说明我尚未定义属性“canvas”: Traceback: File "C:\Python3.6\lib\site-packages\django\core\handlers\exception.py" in inner 35. response = get_response(request) File "C:\Python3.6\lib\site-packages\

我正在使用Django制作一个网页。我试图使使用matplotlib创建的条形图显示在我的网页上,但收到以下错误消息,说明我尚未定义属性“canvas”:

Traceback:

File "C:\Python3.6\lib\site-packages\django\core\handlers\exception.py" in inner
  35.   response = get_response(request)

File "C:\Python3.6\lib\site-packages\django\core\handlers\base.py" in _get_response
  128.  response = self.process_exception_by_middleware(e, request)

File "C:\Python3.6\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.  response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\JNPou\Desktop\Den bæredygtige back-up\kogebog\opskriftssoegning\views.py" in Opskriftsside
  136.  json1 = json.dumps(mpld3.fig_to_dict(bar_plot))

File "C:\Python3.6\lib\site-packages\mpld3\_display.py" in fig_to_dict
  167.  Exporter(renderer, close_mpl=False, **kwargs).run(fig)

File "C:\Python3.6\lib\site-packages\mpld3\mplexporter\exporter.py" in run
  45.   if fig.canvas is None:

Exception Type: AttributeError at /opskriftssoegning/kartoffelsuppe-med-porrer/
Exception Value: 'tuple' object has no attribute 'canvas'
下面是我创建绘图的Python代码:

import matplotlib
matplotlib.use('Agg')

import json
import numpy as np
import matplotlib.pyplot as plt, mpld3
import pandas as pd

[...]

def make_bar_plot():
    height = [1, 2, 3, 3.33, 3.67, 4, 4.33, 4.67, 5, 6, 7, 8, 9, 10, 11, 11.33, 11.67, 12, 12.33, 12.67, 13, 14, 15]
    bars = ('1','2','3','4','5','6','7','8','9','10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23')
    y_pos_prototype = np.arange(len(bars))
    y_pos = pd.Series(y_pos_prototype).to_json(orient='values')
    #y_pos = np.arange(len(bars))

    fig = plt.figure()

    color_set = red_yellow_green_gradient(height)
    # Create bars
    plt.bar(y_pos, height, color=color_set, figure=fig)

    # Create names on the x-axis
    plt.xticks(y_pos, bars, fontsize=16, figure=fig)

    # Add title and axis names
    plt.xlabel('categories', fontsize=18, figure=fig)
    plt.ylabel('values', fontsize=18, figure=fig)

    return fig
创建绘图后,我将其指定给Django视图中的字典:

import os

import json
import numpy as np

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt, mpld3

from .plots import red_yellow_green_gradient, make_bar_plot

[...]

def Opskriftsside(request,slug):
    [...]
    bar_plot = make_bar_plot()
    json1 = json.dumps(mpld3.fig_to_dict(bar_plot)) #THINGS GO WRONG HERE
    context['bar_plot']=bar_plot
    context['json1']=json1
    return render(request,'opskriftssoegning/opskrift.html',context)
最后,这是一段相关的HTML代码:

<div id="fig"></div>
    <script type="text/javascript">
      var json1 = { json.dumps(mpld3.fig_to_dict(fig)) };
      mpld3.draw_figure("fig", json1);
    </script>

var json1={json.dumps(mpld3.fig_to_dict(fig))};
mpld3.绘制图(“图”,json1);

我的问题是:如何将画布分配给pyplot图形?

尝试使用mpl3将pyplot图形导出为html

fig_to_html() 或 save_html() 然后使用jason进行解析,然后将其添加到上下文字典中,然后从python传递到html


一些信息:

并检查您是否在此处获得帮助。我犯了一个愚蠢的错误。我从make_bar_plot()返回了两个变量,而我本应只返回一个,如图所示。我的帖子中没有显示该变量,因为我复制了上一个问题的代码,只更改了解决该问题所需的行数。我以为这就是我所改变的一切。我的帖子可能应该被删除。但是你的回答可能对我仍然有用,所以还是谢谢你!