我正在使用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脚本,输出为图形。问题是,如果我通过命令行运行它,脚本将生成图形作为输出。但是,当我通过浏览器按钮单击运行此脚本时,它生成以下错误:
点击按钮后,通过浏览器运行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
,但没有得到解决。
请建议我错在哪里。