Using MPLD3 to add graphs to Django projects.

I am working on a Django project where I want to get data from CSV and build it on pages built using Django views / templates. Here is the code that I still have:

import matplotlib.pyplot as plt, mpld3

graph = plt.plot([1,2,3,4])
g = mpld3.fig_to_html(graph)
return HttpResponse(g)

However, when I run this, I get an error:

AttributeError: 'list' object has no attribute 'savefig'

Does anyone know where I did wrong, or how I can create graphics that I can add to pages that I already have, and not an MPLD3 page.

+4
source share
2 answers

plt.plot returns a list of Line2D objects, not a curly object.

What do you want to do:

import matplotlib.pyplot as plt, mpld3
fig = plt.figure()
fid=plt.plot([3,1,4,1,5])
mpld3.save_html(fig,"test.html")
mpld3.fig_to_html(fig,template_type="simple")
mpld3.show()
+1
source

write @hck3r, plt.plot Line2D, . :

import matplotlib.pyplot as plt, mpld3

fig = plt.figure()
plt.plot([1,2,3,4])
g = mpld3.fig_to_html(fig)
return HttpResponse(g)
+1

Source: https://habr.com/ru/post/1543907/


All Articles