我正在使用python的matplotlib库来绘制图形。我的程序是检测系统中的异常值。我正在为此制作一个界面。我必须在浏览器中单击按钮显示此图表。这是我的php文件代码: -
<?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>
单击此按钮运行python脚本,将输出显示为图形。问题是,如果我通过命令行运行它,脚本会生成图形作为输出。但是当我通过浏览器按钮单击运行此脚本时,它会生成以下错误:
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
这是我的python代码片段: -
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()
我尝试使用mpld3
但未找到解决方法。
请告知我错过的地方。
答案 0 :(得分:3)
经过大量的R&amp; D,我能够解决它。 这是更新的代码: -
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>')