我使用视图创建带有 matplotlib 的图形。这种观点是这样的,取决于id_projet
论证。
def Graphique_view(request,id_projet):
"""
s'occupe de l'affichage du graphique d'un projet
"""
projet = Projet.objects.get(pk=id_projet)
date_debut = projet.date_debut
semaine_fin = delta_semaine(date_debut,projet.date_fin)
f= plt.figure(figsize=(16,9))
plt.title('graphique')
x1=[]
y1=[]
semaine = 0
ETP = 0
while semaine <= semaine_fin :
compteur = 0
for bilan in Bilan_horaire.objects.filter(projet=projet):
if delta_semaine(date_debut,bilan.date) == semaine :
ETP += bilan.ETP
compteur +=1
bilan_semaine=bilan
if compteur != 0 :
x1.append(bilan_semaine.date)
y1.append(ETP)
semaine+=1
plt.plot(x1,y1,label = 'ETP travaillés')
ETP = 0
for livrable in projet.livrables :
semaine = delta_semaine(date_debut,livrable.dernier_jalon.date_jalon)
for utilisateur in projet.utilisateurs:
ETP += ETP_ressources_livrable.objects.filter(utilisateur=utilisateur).filter(livrable=livrable).order_by('date_modification').reverse()[0].ETP_estime
x=[date_debut,livrable.dernier_jalon.date_jalon,livrable.dernier_jalon.date_jalon]
y=[ETP,ETP,0]
plt.plot(x,y,label=livrable.libelle)
myFmt = mdates.DateFormatter('S%W - %Y')
axes = plt.gca()
axes.xaxis.set_major_locator(mdates.DayLocator(interval=7))
axes.xaxis.set_major_formatter(myFmt)
plt.xticks(rotation = '-50')
plt.xlabel('temps (semaines)')
plt.ylabel('heures (ETP)')
plt.legend()
canvas = FigureCanvasAgg(f)
response = HttpResponse(content_type='image/jpg')
canvas.print_jpg(response)
matplotlib.pyplot.close(f)
return response
网址关联是 projet/<int:id_projet>/graphique
现在,如果尝试在这样的 html 中显示这 2 个图像:
<img src="{% url 'graphique_projet' 9 %}" />
<img src="{% url 'graphique_projet' 10 %}" />
我有那些不是我想要的。
任何帮助将不胜感激。提前致谢 !
我的问题与 Brice 在此处提出的问题相同:如何在一个 django 模板页面中显示多个 matplotlib 图
所以解决办法就是使用RLock()
函数。
这是我现在的看法:
from threading import RLock
verrou = RLock()
def Graphique_view(request,id_projet):
"""
s'occupe de l'affichage du graphique d'un projet
"""
with verrou :
projet = Projet.objects.get(pk=id_projet)
date_debut = projet.date_debut
semaine_fin = delta_semaine(date_debut,projet.date_fin)
f= plt.figure(figsize=(16,9))
plt.title('graphique')
x1=[]
y1=[]
semaine = 0
ETP = 0
while semaine <= semaine_fin :
compteur = 0
for bilan in Bilan_horaire.objects.filter(projet=projet):
if delta_semaine(date_debut,bilan.date) == semaine :
ETP += bilan.ETP
compteur +=1
bilan_semaine=bilan
if compteur != 0 :
x1.append(bilan_semaine.date)
y1.append(ETP)
semaine+=1
plt.plot(x1,y1,label = 'ETP travaillés')
ETP = 0
for livrable in projet.livrables :
semaine = delta_semaine(date_debut,livrable.dernier_jalon.date_jalon)
for utilisateur in projet.utilisateurs:
ETP += ETP_ressources_livrable.objects.filter(utilisateur=utilisateur).filter(livrable=livrable).order_by('date_modification').reverse()[0].ETP_estime
x=[date_debut,livrable.dernier_jalon.date_jalon,livrable.dernier_jalon.date_jalon]
y=[ETP,ETP,0]
plt.plot(x,y,label=livrable.libelle)
myFmt = mdates.DateFormatter('S%W - %Y')
axes = plt.gca()
axes.xaxis.set_major_locator(mdates.DayLocator(interval=7))
axes.xaxis.set_major_formatter(myFmt)
plt.xticks(rotation = '-50')
plt.xlabel('temps (semaines)')
plt.ylabel('heures (ETP)')
plt.legend()
canvas = FigureCanvasAgg(f)
response = HttpResponse(content_type='image/jpg')
canvas.print_jpg(response)
matplotlib.pyplot.close(f)
return response
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句