How to display a line graph in Django?

26 views
Skip to first unread message

ratnadeep ray

unread,
Jul 1, 2020, 12:44:17 AM7/1/20
to Django users
Hi all, 

I have wrote a python program to display the line graph as follows: 

import matplotlib.pyplot as plt

x
= [1,2,3]
y
= [2,4,1]
 

plt
.plot(x, y)
 

plt
.xlabel('x - axis')
plt
.ylabel('y - axis')
 

plt
.title('My first graph!')
 
plt
.show()


Now I need to display the same via Django. So I think I need to write the above code in the view and then redirect the same to the html template file. Am I right?

In that case, how to send the request objects related to the line chart to the template?

Normally, we send in the following format:

def view1(request)


 
return render(request, 'display_report.html', { 'var1': "abc", ...
 
... })

So if we are trying to display the line graph generated by the above code, how to do so ? 

Please share your inputs. 

Thanks. 


Yamen Gamal Eldin

unread,
Jul 1, 2020, 8:42:26 AM7/1/20
to django...@googlegroups.com
There's plotting library in Django.
But if I were u, I would send the data needs plotting to the template, and plot it using a JavaScript library like chart js for small plots or d3 J's for more professional ones.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/827e0d0c-6bbb-4511-af73-9fa379df699do%40googlegroups.com.

shedd tutt

unread,
Jul 1, 2020, 8:56:59 AM7/1/20
to django...@googlegroups.com
Use chartjs

Reply all
Reply to author
Forward
0 new messages