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