show() 不再重绘
我在 Linux 中工作,我不知道为什么使用 python 和 matplotlib 命令只能绘制一次我想要的图表。 我第一次调用 show() 时,绘图被绘制,没有任何问题,但第二次和接下来的情况则不然。
我关闭显示两次调用之间图表的窗口。您知道原因并急于解决吗?
谢谢AFG
from numpy import * from pylab import * data = array( [ 1,2,3,4,5] ) plot(data) [<matplotlib.lines.Line2D object at 0x90c98ac>] show() # this call shows me a plot #..now I close the window... data = array( [ 1,2,3,4,5,6] ) plot(data) [<matplotlib.lines.Line2D object at 0x92dafec>] show() # this one doesn't shows me anything
I am working in linux and I don't know why using python and matplotlib commands draws me only once the chart I want.
The first time I call show() the plot is drawn, wihtout any problem, but not the second time and the following.
I close the window showing the chart between the two calls. Do you know why and hot to fix it?
Thanks AFG
from numpy import * from pylab import * data = array( [ 1,2,3,4,5] ) plot(data) [<matplotlib.lines.Line2D object at 0x90c98ac>] show() # this call shows me a plot #..now I close the window... data = array( [ 1,2,3,4,5,6] ) plot(data) [<matplotlib.lines.Line2D object at 0x92dafec>] show() # this one doesn't shows me anything
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Windows 中,这工作得很好:
你尝试过吗:
有时 matplotlib 会产生一些头痛,因为我们必须记住一些选项是在 matplotlibrc 中设置的(例如后端或交互参数)。如果您使用来自不同编辑器(IDLE-tk、pycrust-wxpython)的 matplotlib 或与脚本交互交互,那么您必须考虑到在一种模式下工作的配置可能会在另一种模式下给您带来问题,并且必须以编程方式进行修改或者使用专用的配置文件。
我给出的示例可以直接运行(并且无需 show()),因为在 matplotlibrc 中我将交互式设置为默认值 True
in windows this works perfect:
did you try with:
sometimes matplotlib produces some headaches because we have to remember that some options are set in matplotlibrc (such as the backend or the interactive parameters). If you use matplotlib from different editors (IDLE-tk, pycrust-wxpython) or alternating interactive with scripting, then you have to take into account that the configuration that works in one mode could give you problems in the other mode and must be modified programmatically or using a dedicated configuration file.
The example I give, works directly (and without show()) because in matplotlibrc I have interactive set to True as default
您的编辑器/IDE 窗口系统和绘图窗口之间可能存在冲突。
解决这个问题的一个很好的方法是使用 IPython。 IPython 是一个很棒的交互式环境,已经解决了这些问题,并且还有许多其他优点。首先,使用命令(从终端窗口)
ipython -pylab
启动 IPython,将其置于交互式 pylab 模式。You likely have conflicts between your editor/IDE windowing system, and your plot windows.
A very good way around this is to use IPython. IPython is a great interactive environment, and has worked out these issues plus has many other advantages. At the beginning, start IPython with the command (from a terminal window)
ipython -pylab
to put it in the interactive pylab mode.我猜你是在 Windows 上的 IDLE 中执行此操作,因为我在那里注意到了同样的问题。
根据我的推断,使用 TkAgg 后端存在问题,该后端附带基本的 Python dist,并且在将 matplotlib 与 IDLE 一起使用时似乎是 matplotlib 的默认后端。它与 IDLE 使用子进程的方式有关,因为如果我使用 -n 选项启动 IDLE(该选项会禁用子进程),则不会出现此问题。在 Windows 上使用 -n 选项以 IDLE 方式启动它的一个简单方法是右键单击文件并选择“以 IDLE 方式打开”。如果你这样做,你应该得到一个 IDLE shell,上面写着
=== 无子进程 ===
就在提示上方。例如,借用 joaquin 解决方案中的代码,您可以尝试这个简单的代码:
然后关闭窗口并再次在控制台中输入最后一行。它在 IDLE 中使用 -n 选项对我有用。
那么你能做什么呢?您始终可以在没有子进程的模式下运行 IDLE,但这样做存在危险。您可以使用不同的 IDE。许多人建议使用 IPython,尽管我自己还没有接受它。您还可以尝试使用不同的 matplotlib 后端。稍后我会尝试一下,因为我一直想知道它是否有效。
I'm guessing that you are doing this in IDLE on Windows because that's where I've noticed this same problem.
From what I've deduced, there is a problem with using the TkAgg backend,which comes with the basic Python dist and appears to be the default for matplotlib, when using matplotlib with IDLE. It has something to do with the way IDLE uses subprocesses because if I start IDLE with the -n option, which disables subprocesses, I don't have this problem. An easy way to start it IDLE with the -n option on Windows is to right click and file and select 'Open with IDLE'. If you do this you should get an IDLE shell which says
=== No Subprocess ===
just above the prompt. For instance, borrowing code from joaquin's solution, you could try this simple code:
then close the window and type the last line into the console again. It works for me in IDLE with the -n option.
So what can you do? You can always run IDLE in the mode without subprocesses, but there are dangers to that. You can use a different IDE. Many people suggest IPython though I'm not sold on it yet myself. You could also try a different backend for matplotlib. I'm going to try that in a little while cause I've been wondering whether it will work.
show()
只能在程序的最后使用一次:它是一个永无止境的循环,用于检查图形窗口中的事件。做你想做的事情的正常方法是:
你可以尝试看看这是否适合你。
或者,您可以尝试更改后端,因为某些后端比其他后端工作得更好:
show()
is only meant to be used once in a program, at the very end: it is a never ending loop that checks for events in the graphic windows.The normal way of doing what you want is:
You could try to see whether this works for you.
Alternatively, you can try to change the backend, as some backends work better than others: