2

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

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Owen Sim
  • 31
  • 1
  • 5
  • I think you need to provide some more information about your system and how you run the code. Mind that `matplotlib.use('Agg')` is explicitely not showing anything (this is the purpose of this backend). `matplotlib.use('TKAgg')` however should show the graph in a window. – ImportanceOfBeingErnest Jun 13 '17 at 09:30
  • check here: [link](https://stackoverflow.com/questions/37604289/tkinter-tclerror-no-display-name-and-no-display-environment-variable) – seralouk Jun 13 '17 at 09:34
  • @ImportanceOfBeingErnest Hi ! i am currently using Putty on my windows system to connect to raspberry pi . I want to be able to display the graph that is shown after typing `sudo python matplot2.py` in the putty terminal . I've tried using `matplotlib.use('TKAgg')` but it still returns the same error. But when i use `matplotlib.use('Agg')` the terminal just skips to the next line and nothing is shown like you mentioned . – Owen Sim Jun 15 '17 at 07:36
  • @ImportanceOfBeingErnest after trying the suggestion below , i downloaded Xming & allowed SSH forwarding on putty . i used the exact same code with `matplotlib.use('TKAgg')` and error :`_tkinter.TclError: couldn't connect to display "localhost:10.0"` is returned. – Owen Sim Jun 15 '17 at 07:39
  • I had the same problem using Xming on windows 10. I have to lunch manually Xming before opening putty every time I reboot. – Stefano Cremona Feb 01 '20 at 12:15

1 Answers1

3

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

mjoppich
  • 3,207
  • 1
  • 11
  • 13
  • 2
    I've tried to use XMing instead , and now , the error i got is : `_tkinter.TclError: couldn't connect to display "localhost:11.0" ` – Owen Sim Jun 15 '17 at 03:17
  • Can you double-check that you enabled X11 forwarding when connecting to your raspberry pi ( http://www.geo.mtu.edu/geoschem/docs/putty_install.html ) What happens if you start some other program which has a GUI (e.g. gedit?). Using `matplotlib.use('agg')` limits your output to files, hence no "interactive" window would open. – mjoppich Jun 15 '17 at 08:49
  • I tried to open gedit , xeyes , xclock and they all work fine. But when i use `matplotlib.use('TKAgg')`the error will appear . However when the `matplotlib.use('Agg')` is used. The file would work and i can save a png image. But at the same time, i need to have the "interactive window" out , therefore i'm trying to find a way to do it. – Owen Sim Jun 15 '17 at 09:06
  • And what happens, if you don't change the matplotlib backend? I edited my answer and performed a test on my own raspberry pi (which currently is 600km away, so I used putty to ssh to it) and it worked by just enabling X11 forwarding in putty and having VcXsrv running. I used the source code as mentioned here http://matplotlib.org/examples/api/barchart_demo.html . – mjoppich Jun 15 '17 at 13:23
  • and what was the problem? – mjoppich Jun 16 '17 at 06:54
  • i set the `matplotlib.use('Agg')` in one of the files and set it as default backend and forgot about it – Owen Sim Jun 16 '17 at 08:19
  • so apparently my solution of X11 forwarding is correct. could you mark that answer then as solution or upvote? imho it's a bit stupid to provide a correct answer and yet have it downvoted -_- – mjoppich Jun 16 '17 at 14:54