我正在尝试使用matplot lib绘制图形,并且没有显示窗口:
我使用matplotlib从轮廓演示程序中获取了这段代码。
import matplotlib
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)
# Create a simple contour plot with labels using default colors. The
# inline argument to clabel will control whether the labels are draw
# over the line segments of the contour, removing the lines beneath
# the label
plt.figure()
CS = plt.contour(X, Y, Z)
plt.clabel(CS, inline=1, fontsize=10)
plt.title('Simplest default with labels')
plt.show()
现在我希望plt.show()显示一个情节,但是当我运行程序时,我没有看到情节,而是收到一条错误消息:
import: unable to grab mouse `': Resource temporarily unavailable @ error/xwindow.c/XSelectWindow/9047.
import: unable to grab mouse `': Resource temporarily unavailable @ error/xwindow.c/XSelectWindow/9047.
import: unable to grab mouse `': Resource temporarily unavailable @ error/xwindow.c/XSelectWindow/9047.
import: unable to grab mouse `': Resource temporarily unavailable @ error/xwindow.c/XSelectWindow/9047.
./plotter.py: line 7: matplotlib.rcParams[xtick.direction]: command not found
./plotter.py: line 8: matplotlib.rcParams[ytick.direction]: command not found
./plotter.py: line 10: delta: command not found
./plotter.py: line 11: syntax error near unexpected token `('
./plotter.py: line 11: `x = np.arange(-3.0, 3.0, delta)'
有关如何解决此问题的任何想法?
感谢您的时间。
答案 0 :(得分:3)
代码很好,如果我没弄错的话你已经把它作为shell脚本执行了。你应该用python interpeter执行它。
操作终端并运行python script-name.py
如果安装了matplotlib和numpy,你应该没问题。
答案 1 :(得分:0)
matplotlib在标准的python解释器中很棘手。 ipython与matplotlib有更好的集成,所以你可能想尝试使用它而不是标准的python命令行解释器。它会让你的生活在这么多方面变得更轻松。