I am using matplotlib library of python for plotting graphs. My program is to detect the outliers in the system. I am making an interface for this. I have to show this graph in browser on clicking the button. Here is my php file code :-
<?php
if (isset($_POST['button']))
{
echo shell_exec("python3 /var/www/html/python/anomalies.py 2>&1");
}
?>
<html>
<body>
<center>
<form method="post">
<table>
<tr>
<td>
Enter CSV file path:
</td>
<td>
<input type="text" name="path">
</td>
</tr>
</table>
<p>
<button name="button">Identify Anomalies</button>
</p>
</form>
</center>
</body>
on clicking this button a python script runs which gives the output as graph. The problem is that if i run it by command line, the script generates the graph as output. but when i run this script through browser button click, it generates the following error:
Traceback (most recent call last): File "/var/www/html/python/anomalies.py", line 72, in plt.xlabel('RelativeTime (ms)') File "/usr/local/lib/python3.4/dist-packages/matplotlib/pyplot.py", line 1512, in xlabel return gca().set_xlabel(s, *args, **kwargs) File "/usr/local/lib/python3.4/dist-packages/matplotlib/pyplot.py", line 984, in gca return gcf().gca(**kwargs) File "/usr/local/lib/python3.4/dist-packages/matplotlib/pyplot.py", line 601, in gcf return figure() File "/usr/local/lib/python3.4/dist-packages/matplotlib/pyplot.py", line 548, in figure **kwargs) File "/usr/local/lib/python3.4/dist-packages/matplotlib/backend_bases.py", line 161, in new_figure_manager return cls.new_figure_manager_given_figure(num, fig) File "/usr/local/lib/python3.4/dist-packages/matplotlib/backends/_backend_tk.py", line 1044, in new_figure_manager_given_figure window = Tk.Tk(className="matplotlib") File "/usr/lib/python3.4/tkinter/__init__.py", line 1854, in __init__ self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use) _tkinter.TclError: no display name and no $DISPLAY environment variable
Here is my python code snippet:-
plt.xlabel('R_Time (ms)')
plt.ylabel('A_Time (ms)')
plt.title('R-A Combinations')
plt.plot(tr_data[:,0],tr_data[:,1],'bx')
plt.plot(tr_data[outliers,0],tr_data[outliers,1],'ro')
plt.show()
I tried using mpld3
but found no resolution.
Please suggest where i am missing it in.
After lot of R & D, i am able to solve it. Here is the updated code :-
import matplotlib
matplotlib.use('WebAgg')
import matplotlib.pyplot as plt,mpld3
fig1 = plt.figure()
plt.xlabel('RelativeTime (ms)')
plt.ylabel('AbsoluteTime (ms)')
plt.title('R-A Combinations')
plt.plot(tr_data[:,0],tr_data[:,1],'bx')
plt.plot(tr_data[outliers,0],tr_data[outliers,1],'ro')
print ('<HTML><HEAD><TITLE>Python Matplotlib Graph</TITLE></HEAD>')
print ('<BODY>')
print ('<CENTER>')
print ('<br><br>')
print ('<H3>Graph</H3>')
print (mpld3.fig_to_html(fig1, d3_url=None, mpld3_url=None, no_extras=False, template_type='general', figid=None, use_http=False))
print ('<br>')
print ('</CENTER>')
print ('</BODY>')
print ('</html>')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With