简体   繁体   English

Visual Stdio Code、PyCharm 和 Thonny 在运行简单的 matplotlib 演示代码时表现不同

[英]Visual Stdio Code, PyCharm, and Thonny behave differently running simple matplotlib demo code

I want to use Python's matplotlib to display some sensor data in 3D and in real time.我想使用 Python 的 matplotlib 以 3D 和实时方式显示一些传感器数据。 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.如果你愿意的话,一个“Hello World”。

I started with a fresh install of PyCharm on my reasonably fresh Windows 11 PC.我开始在我相当新的 Windows 11 PC 上全新安装 PyCharm。 This setup works fine for things that don't involve matplotlib.此设置适用于不涉及 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.过了一会儿,我得出结论,PyCharm 只是没有按我预期的方式工作,为了好玩,它使用 Thonny 运行代码(是的,我有几个 Raspberry Pi),它按预期工作。 Just because I could, I tried the same code using Visual Studio Code 2022. It didn't work the same way PyCharm didn't.正因为可以,我使用 Visual Studio Code 2022 尝试了相同的代码。它的工作方式与 PyCharm 不同。

PyCharm and VS Code would output the printed "value" and open the "figure" window, but would not display the sine wave plot. PyCharm 和 VS Code 将输出打印的“值”并打开“图形”窗口,但不会显示正弦波图。 Eventually the "figure" window would say "not responding" for both.最终,“数字”窗口会对两者都显示“没有响应”。 The Thonny output was as expected, and there was no "not responding" message. Thonny 输出与预期一致,并且没有“未响应”消息。

The only difference I see is that the PyCharm and VS Code use virtual environments, and Thonny does not.我看到的唯一区别是 PyCharm 和 VS Code 使用虚拟环境,而 Thonny 没有。

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编辑——此外,从命令行运行代码(Windows 终端(管理员)的行为与 PyCharm 和 VS Code 相同。/编辑

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()

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).有办法绕过它并为他们提供这种访问权限( https://virtualizationreview.com/articles/2017/02/08/graphical-programs-on-windows-subsystem-on-linux.aspx ),但通常很多使用 CMD 之类的东西从 Windows 本地运行程序更容易(或者似乎 Thonny 为您工作)。 You also might want to consider trying out Jupyter Notebook depending on what you're doing.您可能还想根据您的工作考虑试用 Jupyter Notebook。 Using virtual subsystems like WSL has given me lots of problems with this in the past.过去,使用像 WSL 这样的虚拟子系统给我带来了很多问题。

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.以下示例可在所有 VS Code、PyCharm 和 Thonny IDE 中可靠运行。

That snealy little "plt.pause(1e-17)" seems to make a lot of difference.那个狡猾的小“plt.pause(1e-17)”似乎有很大的不同。

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()

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

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