问题描述
我开始使用面向初学者的此教程来学习MatPlotLib.这是第一个例子.
I started to learn MatPlotLib using this tutorial for beginners. Here is the first example.
from pylab import *
X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X)
如果我将这3行写到我的python文件中并在命令行中执行(通过键入python file_name.py
),则什么都不会发生.没有错误信息,没有情节.
If I write these 3 lines into my python file and execute it in the command line (by typing python file_name.py
), nothing happens. No error message, no plot.
有人知道为什么我看不到情节吗?
Does anybody know why I do not see the plot?
添加
我当然需要使用show
.但是,即使我添加以下3行:
Of course I need to use show
. But even if I add the following 3 lines:
plot(X,C)
plot(X,S)
show()
它仍然不产生任何东西.
it still does no generate anything.
添加
这是我现在使用的行:
import pylab as p
C = [1,2,3,4]
S = [10, 20, 30, 10]
p.plot(C,S)
p.show()
我仍然有相同的结果(什么都没有).
I still have the same result (nothing).
推荐答案
后端可能有问题.什么是输出python -c 'import matplotlib; import matplotlib.pyplot; print(matplotlib.backends.backend)'
?
It could be a problem with the backend.What is the output of python -c 'import matplotlib; import matplotlib.pyplot; print(matplotlib.backends.backend)'
?
如果它是"agg"后端,则您看到的是预期的行为,因为它是一个非交互式后端,不会在屏幕上显示任何内容,但可以使用plt.savefig(...).您应该切换到例如TkAgg或Qt4Agg才能使用show
.您可以在matplotlib.rc文件中进行操作.
If it is the 'agg' backend, what you see is the expected behaviour as it is a non-interactive backend that does not show anything to the screen, but work with plt.savefig(...).You should switch to, e.g., TkAgg or Qt4Agg to be able to use show
. You can do it in the matplotlib.rc file.
@shashank:我在12.04和12.10上都运行了matplotlib,没有问题.在这两种情况下,我都使用Qt4Agg后端.如果您未设置matplotlibrc,则使用默认后端.我敢肯定,对于精确Matplotlib repo是使用TkAgg构建的.如果Quantal版本是使用例如Agg,那可以解释差异
@shashank: I run matplotlib both on 12.04 and 12.10 without problems. In both cases I use the Qt4Agg backend. If you don't have the matplotlibrc set, the default backend is used.I'm sure that for Precise matplotlib repo was built with TkAgg. If the Quantal version has been built with e.g. Agg, then that would explain the difference
这篇关于为什么matplotlib不作图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!