I want to use Python's matplotlib to display some sensor data in 3D and in real time. My starting place was to try do just plot a sine wave from one of the many examples from the web. A "Hello World" if you will.
I started with a fresh install of PyCharm on my reasonably fresh Windows 11 PC. This setup works fine for things that don't involve matplotlib. After a while I came to the conclusion that PyCharm was just not working the way I expected, and for the fun of it ran the code using Thonny (yeah, I have several Raspberry Pis), and it worked as expected. Just because I could, I tried the same code using Visual Studio Code 2022. It didn't work the same way PyCharm didn't.
PyCharm and VS Code would output the printed "value" and open the "figure" window, but would not display the sine wave plot. Eventually the "figure" window would say "not responding" for both. The Thonny output was as expected, and there was no "not responding" message.
The only difference I see is that the PyCharm and VS Code use virtual environments, and Thonny does not.
I'll admit to not being the sharpest knife in the drawer, and will be appreciative of suggestions.
Edit -- Also, running the code from the command line (Windows Terminal (Admin) acts the same way as for PyCharm and VS Code. /Edit
The code in question is:
import numpy as np
import time
import matplotlib.pyplot as plt
figure, ax = plt.subplots(figsize=(4,5))
x = np.linspace(0, 20, 80)
y = np.sin(x)
plt.ion()
plot1, = ax.plot(x, y)
plt.xlabel("X-Axis",fontsize=18)
plt.ylabel("Y-Axis",fontsize=18)
for value in range(25):
update_y_value = np.sin(x-2.5*value)
plot1.set_xdata(x)
plot1.set_ydata(update_y_value)
figure.canvas.draw()
figure.canvas.flush_events()
time.sleep(0.05)
print(value)
plt.show()
CodePudding user response:
I was confused by this at one point, basically for displaying graphics on the screen it's much easier if running directly on the machine since virtual terminals don't have access to your screen by default. There are ways to get around it and give them this access (https://virtualizationreview.com/articles/2017/02/08/graphical-programs-on-windows-subsystem-on-linux.aspx), but often it's much easier to just run the program from Windows natively using something like CMD (or it seems like Thonny worked for you). You also might want to consider trying out Jupyter Notebook depending on what you're doing. Using virtual subsystems like WSL has given me lots of problems with this in the past.
CodePudding user response:
I finally found an example that I can use as a starting place for what I really want to do. The following sample runs reliably in all of VS Code, PyCharm and Thonny IDEs.
That snealy little "plt.pause(1e-17)" seems to make a lot of difference.
import numpy as np
import matplotlib.pyplot as plt
xdata = []
ydata = []
axes = plt.gca()
axes.set_xlim(0, 100)
axes.set_ylim(-2, 2)
line, = axes.plot(xdata, ydata, 'r-')
x = np.linspace(0, 19, 100)
y = np.sin(x)
plt.xlabel("X-Axis", fontsize=18)
plt.ylabel("Y-Axis", fontsize=18)
for value in range(100):
xdata.append(value)
ydata.append(y[value])
line.set_xdata(xdata)
line.set_ydata(ydata)
plt.pause(1e-17)
plt.show()