使用 Visual Studio 的 Python 工具在 Visual Studio 中使用 Matplotlib 绘图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28678762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Plotting with Matplotlib in Visual Studio using Python Tools for Visual Studio
提问by kdemirtas_
I am new to using PTVS for my Python code. I previously used Spyder as it came along with the Anaconda distribution. Here is the issue I am having.
我是在 Python 代码中使用 PTVS 的新手。我以前使用过 Spyder,因为它与 Anaconda 发行版一起出现。这是我遇到的问题。
I am trying to create two plots, and show them in separate windows at the same time. A simple example is.
我正在尝试创建两个图,并同时在不同的窗口中显示它们。一个简单的例子是。
import matplotlib.pyplot as plt
plt.plot([1,2,3,4,5])
plt.show()
plt.plot([2,2,2,2,2])
plt.show()
But the second plot does not show up unless I close the first plot. Similarly for a larger script, the rest of the code chunk after plt.show() does not execute if I don't close the plot. I tried executing without debugging and it does not work. However, when I run the same code in Ipython interactive window, all the code executes and I can see both plots as inline, yet it is not what I want. I want to all the code to run creating as many plots as needed in different windows without me interrupting like closing the plot for the rest of the code to run. I can still do it in Spyder, but I want to switch to Visual Studio completely and this issue is bugging me.
但是除非我关闭第一个图,否则第二个图不会出现。同样,对于较大的脚本,如果我不关闭绘图,则 plt.show() 之后的其余代码块不会执行。我尝试在没有调试的情况下执行,但它不起作用。但是,当我在 Ipython 交互式窗口中运行相同的代码时,所有代码都会执行并且我可以看到两个图都内联,但这不是我想要的。我希望所有代码都能运行,在不同的窗口中根据需要创建尽可能多的图,而不会像关闭图以运行其余代码那样中断。我仍然可以在 Spyder 中做到这一点,但我想完全切换到 Visual Studio,这个问题困扰着我。
Any help would be appreciated. Thanks in advance.
任何帮助,将不胜感激。提前致谢。
回答by ljetibo
Now I don't know how this would work in VS but whenever I have to make multiple plots in interactive window (without saving them) and I want to display all of them together this is what I do:
现在我不知道这在 VS 中如何工作,但是每当我必须在交互式窗口中制作多个绘图(不保存它们)并且我想将它们全部显示在一起时,这就是我所做的:
fig, ax = plt.subplots()
ax.plot([1,2,3,4,5])
fig1, ax1 = plt.subplots()
ax1.plot([1,2,3,4,5])
plt.show()
do notice that you will be stuck in place (still) after the plt.show(). To have an interactive window that doesn't obstruct the rest of your code, you have to use IPython, or some other tool which enables async plotting and calculations. However directly from Python interpreter, you're going to have to wait until you terminate that line.
请注意,在plt.show(). 要拥有一个不妨碍其余代码的交互式窗口,您必须使用 IPython 或其他一些支持异步绘图和计算的工具。但是,直接从 Python 解释器中,您将不得不等到终止该行。
Additionally you could turn on interactive mode by doing plt.ion()beforeanything else. This enables direct view to your plot, which updates after you issue commands pertaining to it.
此外,您可以plt.ion()先执行其他操作来打开交互模式。这可以直接查看您的绘图,在您发出与其相关的命令后会更新。
I believe that is the actual command you're looking for. Here's the manual
我相信这是您正在寻找的实际命令。这是手册
回答by erik-e
I haven't done this in Visual Studio but with Matplotlib you can use a non-blocking call to show. The drawmethod does not block but if you don't end the calls by doing a showthen the graph window will immediately close.
我还没有在 Visual Studio 中这样做过,但是使用 Matplotlib,您可以使用非阻塞调用show. 该draw方法不会阻塞,但如果您不通过执行 a 来结束调用,show则图形窗口将立即关闭。
import matplotlib.pyplot as plt
plt.figure(1)
plt.plot([1,2,3,4,5])
plt.show(block=False)
# Create a new figure to show the extra information.
# http://matplotlib.org/users/pyplot_tutorial.html#working-with-multiple-figures-and-axes
plt.figure(2)
plt.plot([2,2,2,2,2])
plt.show()
Here is a related questionon the same topic.
这是关于同一主题的相关问题。
Personally I use subplotsas shown in the matplotlibexamples.
我个人使用subplots如matplotlib示例中所示。
回答by Pavel Minaev
Are you trying to get this work in your own script, or in the Python Interactive window?
您是想在自己的脚本中还是在 Python 交互窗口中完成这项工作?
If it's the latter, then you should enable IPython mode for it in Tools -> Options -> Python Tools -> Interactive Windows -> Interactive Mode (the default is "Standard"; change it to "IPython", and also make sure that you have the right Python selected in the "Show settings for" combo box if you have more than one installed). Then you won't need plt.showat all, as plt.plotwill immediately display the plot inline directly inside the Interactive window.
如果是后者,那么你应该在工具 -> 选项 -> Python 工具 -> 交互窗口 -> 交互模式中为它启用 IPython 模式(默认为“标准”;将其更改为“IPython”,并确保如果您安装了多个 Python,则在“显示设置”组合框中选择了正确的 Python)。那么您将根本不需要plt.show,因为plt.plot它将立即直接在交互式窗口内直接显示内联绘图。
回答by Simon Ldj
I can suggest you an Open source tool I been working on for similar reasons. I wanted to see plots one after the other while debugging, without dealing with closing windows or specific environment...
我可以向您推荐一个我出于类似原因一直在研究的开源工具。我想在调试时一个接一个地查看绘图,而不需要处理关闭窗口或特定环境...
This answer is not helping you directly with Matplotlib but suggesting an alternative way to solve same issues. The problems I find with Matplotlib or IPython and similar tools are that they usually very restricted to certain environment (like in your case, the IDE). Also those tools usually hard to control to your own display requirements (or require much time).
这个答案并没有直接帮助您使用 Matplotlib,而是提出了解决相同问题的替代方法。我发现 Matplotlib 或 IPython 和类似工具的问题是它们通常非常受限于某些环境(例如在您的情况下,IDE)。此外,这些工具通常难以控制到您自己的显示要求(或需要很多时间)。
HypnoLogcan help you plot any data from Python or any other language. It will display the plots in a web browser, at the same page one after the other (like a log). As HypnoLog use the browser to display the plots, you can easily use any other tools avialble on the web to display plots or create your own.
HypnoLog可以帮助您绘制来自 Python 或任何其他语言的任何数据。它将在 Web 浏览器中一个接一个地显示在同一页面上的图(如日志)。由于 HypnoLog 使用浏览器来显示图,因此您可以轻松地使用网络上可用的任何其他工具来显示图或创建自己的图。
Specifically for Python there is already a language wrapper, see HypnoLog-Python. And to plot numerical arrays you can use the built-in visualization plot.
专门针对 Python 已经有一个语言包装器,请参阅HypnoLog-Python。要绘制数值数组,您可以使用内置的可视化plot。
This how it looks like in Python:
这是它在 Python 中的样子:
import hypnolog as HL
HL.log([1,2,3,4,5], 'plot');

