Hi,
I use Django for web applications, and Pandas for data management. I try to use Pandas plot for introduce graph in mi web pages.
When I use de code below in the browser directly, it's work fine, but when I put into de html, this only work with one chart. If only put one graph, also work fine, but with two or more charts in the same html archive, the result is aleatory.
I think that the problem is that Pandas plot() use plt(pyplot) that remains between request.
In this case, the browser-server request asynchronous make two or mor request at the same time, with extranger results.
I think too that the solution is in the use of independ figure in each request like (
fig = Figure() and not fig=plt.figure(...)) but seems that Pandas plot use plt internally.
So, somebody know how to use Pandas plot() in Django, with multiples request?.
Best Regards
----------------------------------------------------------------------------------------------------------------------------------------------------------
The code is (only the important):
In urls.py:
urlpatterns = patterns('',
url(r'^$', 'myapp.views.home', name='home'),
url(r'^charts/$', 'Agricolae.views.Pandas_Graph'), #/1/ indica chars tipo evolución
)
In myapp.views:
import matplotlib.pyplt as plt
import pandas as pd
import numpy as np
def home(request):
return render_to_response("index.html",locals())
def Pandas_Graph(request):
#create de figure
fig=plt.figure(figsize=(6, 4.5), dpi=80,facecolor='w', edgecolor='w')
ax=fig.add_subplot(111)
#create de Dataframe and plot it
df=pd.DataFrame({'y':np.random.randn(10),'z':np.random.randn(10)},
index=pd.period_range('1-2000',periods=10))
df.plot()
plt.legend(loc='best')
#pass the figure to dinamical-like-png
canvas=FigureCanvas(fig)
response=HttpResponse(content_type='image/png')
canvas.print_png(response)
return response
In index.html:
<head>
<title>myapp</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
</head>
<body>
<br>
The first graph<br>
<img src="/charts/">
<br>
The second graph<br>
</body>
</html>