在 python (matplotlib) 中绘制矢量场

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1843194/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:11:53  来源:igfitidea点击:

Plotting vector fields in python (matplotlib)

pythonmatplotlib

提问by devoured elysium

I found this code on http://matplotlib.sourceforge.net/examples/pylab_examples/quiver_demo.html

我在http://matplotlib.sourceforge.net/examples/pylab_examples/quiver_demo.html上找到了这段代码

from pylab import *
from numpy import ma

X,Y = meshgrid( arange(0,2*pi,.2),arange(0,2*pi,.2) )
U = cos(X)
V = sin(Y)

#1
figure()
Q = quiver( U, V)
qk = quiverkey(Q, 0.5, 0.92, 2, r' \frac{m}{s}$', labelpos='W',
               fontproperties={'weight': 'bold'})
l,r,b,t = axis()
dx, dy = r-l, t-b
axis([l-0.05*dx, r+0.05*dx, b-0.05*dy, t+0.05*dy])

title('Minimal arguments, no kwargs')

Now, how can I see this graphic? Better yet, how can I save it to a file as, let's, say, JPEG? The code seems to run but I can't see anything happening.

现在,我怎样才能看到这个图形?更好的是,我怎样才能将它保存到一个文件中,比如说,JPEG?代码似乎在运行,但我看不到任何事情发生。

采纳答案by unutbu

Stick show()at the end of the script. Or to save it to a jpg file, put

坚持show()在脚本的末尾。或者将其保存为 jpg 文件,输入

savefig('output.jpg')
show()

Be sure to put the savefig()command before the show().

确保将savefig()命令放在show().

回答by doffles

Ideally, you would type this in an interactive shell ( like EPD Python's PyLab ) . Otherwise, you need to explicitly invoke the show()command

理想情况下,您可以在交互式 shell(如 EPD Python 的 PyLab)中键入它。否则,您需要显式调用show()命令

回答by codelogic

You need to callshow()or savefig().

您需要致电show()savefig()