Owen Sim
Owen Sim

Reputation: 31

Matplotlib graph not showing

My project requires me to open Raspberry Pi's terminal using putty and Connectify hotspot on my Windows System to plot and show the graph. However , the graph was only able to be shown on my Raspberry Pi monitor but not my Window's one. Here's the code that i used :

import pymysql
import matplotlib
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd

conn = pymysql.connect(host="localhost", user="root", passwd="123456", db="XXX")

cur = conn.cursor()

query = """
SELECT data,time
FROM sensordata
WHERE time >= "2017-05-21"
  AND time < "2017-05-23"
"""


cur.execute(query)

data = cur.fetchall()

cur.close()
conn.close()

time,data= zip(*data)
plt.plot(data,time)

plt.title("XXX ")
plt.xlabel("Time & Date ")
plt.ylabel("Strength")
fig = plt.gcf()

fig.set_size_inches (55,27.5)
plt.grid(True)
plt.draw()
fig.savefig('test.png' ,dpi=100)
plt.show()

The error i received when i tried to run it on the putty terminal is :

Traceback (most recent call last):
  File "matplot2.py", line 27, in <module>
    plt.plot(data,time)
  File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 3092, in plot
    ax = gca()
  File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 828, in gca
    ax =  gcf().gca(**kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 462, in gcf
    return figure()
  File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 435, in figure
    **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 81, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 89, in new_figure_manager_given_figure
    window = Tk.Tk()
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1813, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

I've tried using both matplotlib.use('Agg') and matplotlib.use('TKAgg') from some solutions i read but it did not solve my issue. Hope that someone would be able to solve my issue so that i can display the graph on my Window's monitor ... Thanks in advance

Upvotes: 2

Views: 8482

Answers (1)

mjoppich
mjoppich

Reputation: 3577

The error '_tkinter.TclError: no display name and no $DISPLAY environment variable' suggests that there is a problem with your X11 server.

I do not know from which host you ssh on your Raspberry, but you must run an X-Server on Windows (such as VcXsrv ). Then make sure to allow X11 forwarding, e.g. ssh -X or even ssh -Y. When using putty make sure to set Enable X11 forwarding under Connection -> SSH -> X11.

If all is set up, running the matplotlib bar chart demo (see comments) should look like this:

enter image description here (no image since someone downvoted the answer for whatever reason)

If your problem persists, you could try to use MobaXTerm (on Windows) which comes with an X Server and should work out of the box.

If you are running VcXSrv, for me I have to export my $DISPLAY variable by running the following code in your shell: export DISPLAY=localhost:0.0

Upvotes: 3

Related Questions