简体   繁体   English

在浏览器中显示matplotlib图

[英]Display matplotlib graph in browser

I am using matplotlib library of python for plotting graphs. 我正在使用python的matplotlib库来绘制图形。 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文件代码: -

<?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. 单击此按钮时,将运行python脚本,将输出显示为图形。 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:- 这是我的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()

I tried using mpld3 but found no resolution. 我尝试使用mpld3但没有找到解决方案。 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>')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM