Im using python 3.4 and Django 1.8. I want to "print" the matplotlib result in a Django template. I achieve this a few days ago, so I continue in other things of my Django application. Now I donβt know why, I'm going to show the result to a friend, and my matplotlib chart template now shows a big code! I do not know why this is happening, because my view does not change anything when he showed the correct schedule! Please help me!
This is my opinion!
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})
Of course, in my graphics.html there is a variable called {{graphic}} inside blockcontent!
This showed the correct result in my template! What's happening? Now sometimes, when I run my template, it shows large code or just shows me this django error:
Exception value:
main thread is not in main loop
Exception Location: C: \ Python34 \ lib \ site-packages \ matplotlib \ backends \ tkagg.py in blit, line 17
Help!