Python 将Matplotlib转换为Django模板
我正在使用python 3.4和Django 1.8。我想在Django模板中“打印”matplotlib结果。我几天前就达到了这一点,所以我继续我的Django应用程序的其他内容。现在,我不知道为什么,我要向一个朋友展示结果,我的带有matplotlib图的模板,现在显示了一个大代码!我不知道为什么会发生这种情况,因为我的观点在显示正确图表时没有任何变化!请帮帮我 这是我的观点Python 将Matplotlib转换为Django模板,python,django,matplotlib,Python,Django,Matplotlib,我正在使用python 3.4和Django 1.8。我想在Django模板中“打印”matplotlib结果。我几天前就达到了这一点,所以我继续我的Django应用程序的其他内容。现在,我不知道为什么,我要向一个朋友展示结果,我的带有matplotlib图的模板,现在显示了一个大代码!我不知道为什么会发生这种情况,因为我的观点在显示正确图表时没有任何变化!请帮帮我 这是我的观点 from django.shortcuts import render from matplotlib import
from django.shortcuts import render
from matplotlib import pylab
from pylab import *
import PIL
import PIL.Image
import io
from io import *
def graphic(request):
pos = arange(10)+ 2
barh(pos,(1,2,3,4,5,6,7,8,9,10),align = 'center')
yticks(pos,('#hcsm','#ukmedlibs','#ImmunoChat','#HCLDR','#ICTD2015','#hpmglobal','#BRCA','#BCSM','#BTSM','#OTalk'))
xlabel('Popularity')
ylabel('Hashtags')
title('Hashtags')
subplots_adjust(left=0.21)
buffer = io.BytesIO()
canvas = pylab.get_current_fig_manager().canvas
canvas.draw()
graphIMG = PIL.Image.fromstring('RGB', canvas.get_width_height(), canvas.tostring_rgb())
graphIMG.save(buffer, "PNG")
content_type="Image/png"
buffercontent=buffer.getvalue()
graphic = (buffercontent ,content_type)
pylab.close()
return render(request, 'graphic.html',{'graphic':graphic})
当然,在my graphic.html中,blockcontent中有一个名为{{graphic}}的变量
这在我的模板中显示了正确的结果!发生了什么事?
现在,有时当我运行模板时,它会显示一个大代码,或者只是显示这个django错误:
异常值:主线程不在主循环中 异常位置:blit中的C:\Python34\lib\site packages\matplotlib\backends\tkagg.py,第17行 救命啊 编辑: 试一试
graphic = cStringIO.StringIO()
canvas.print_png(graphic)
return render(request, 'graphic.html',{'graphic':graphic})
必须指定图像为二进制字符串:
<img src="data:image/png;base64,{{graphic|safe}}">
或者将其保存到文件系统并提供路径
或者,您可以使用html+javascript将绘图直接嵌入到模板中,然后动态生成并提供良好的功能。最终解决方案是创建一个特殊视图,在空模板中返回matplotlib绘图,如下所示:
def grafico (rquest):
pos = arange(10)+ 2
barh(pos,(1,2,3,4,5,6,7,8,9,10),align = 'center')
yticks(pos,('#hcsm','#ukmedlibs','#ImmunoChat','#HCLDR','#ICTD2015','#hpmglobal','#BRCA','#BCSM','#BTSM','#OTalk'))
xlabel('Popularidad')
ylabel('Hashtags')
title('Gráfico de Hashtags')
subplots_adjust(left=0.21)
buffer = io.BytesIO()
canvas = pylab.get_current_fig_manager().canvas
canvas.draw()
graphIMG = PIL.Image.fromstring('RGB', canvas.get_width_height(), canvas.tostring_rgb())
graphIMG.save(buffer, "PNG")
pylab.close()
return HttpResponse (buffer.getvalue(), content_type="Image/png")
下一步是将以下内容放入模板:
<img src="url_of_the_graphic_view">
就这些
def show(request):
x = np.arange(10)
y = x
fig = plt.figure()
plt.plot(x, y)
canvas = fig.canvas
buf, size = canvas.print_to_buffer()
image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
buffer=io.BytesIO()
image.save(buffer,'PNG')
graphic = buffer.getvalue()
graphic = base64.b64encode(graphic)
buffer.close()
return render(request, 'graphic.html',{'graphic':graphic})
在模板中:
<img src="data:image/png;base64,{{ graphic|safe }}">
我有:
matplotlib==3.0.2和Django==2.1.4在使用上述答案后,我也有一个损坏的图标图像,我通过从graphic base64二进制表示中删除
b'
和'
来修复它:
返回render(请求'graphic.html',{'graphic':str(graphic)[2:-1]})
尝试{{{graphic | safe}}
@lana我可以在我的html模板中进行更改,但它不能解决问题!谢谢我尝试使用:现在我的模板中只有一个损坏的图像图标!我是python新手,所以我不知道我需要在我的视图中将渲染行更改为:return render(request,'graphic.html',{'graphic | safe':graphic}){safe函数应该在模板中:)你能在回答中添加图像html代码吗?{%extends“base.html”%}{%block title%}索引{%endblock%}{%block subtitle%}blablablablablablabla{%endblock%}{%block graphic%}{%endblock%}当我的代码停止工作时,我正在django静态文件的设置中工作,这可能是我的问题的原因吗?啊,好的,那么你以前是将其保存到文件系统中的。因此,是的,你也可以将其保存到某个地方,并确保目录位于静态文件路径中。或者签出我的编辑以内联显示它。请尝试避免仅使用dumping代码作为答案,并尝试解释它的作用和原因。对于没有相关编码经验的人,您的代码可能不明显。请编辑您的答案以包含抱歉。尝试使用“graphic=base64.b64encode(graphic)”,用base64编码图形。然后PNG可以在模板中解码。这样,每页只能显示1个绘图…对吗(
<img src="data:image/png;base64,{{ graphic|safe }}">