【发布时间】:2018-02-23 23:10:02
【问题描述】:
我正在测试我在 matplotlib (https://matplotlib.org/gallery/mplot3d/subplot3d.html?highlight=matplotlib%20pyplot%20plot) 上找到的代码来测试我添加了这一行的 3d 图形 ->
import matplotlib
matplotlib.use('PS') # generate postscript output by default
之前
import matplotlib.pyplot as plt
在我编译程序时修复 RunTimeError
编辑:如果我在没有这两行的情况下运行,我会得到这个error
但是,程序运行但没有显示图表。我找了很多地方都找不到解决办法
我正在使用 virtualenv
点子列表:
cycler (0.10.0)
matplotlib (2.1.2)
numpy (1.14.1)
pip (9.0.1)
pyparsing (2.2.0)
python-dateutil (2.6.1)
pytz (2018.3)
setuptools (38.5.1)
six (1.11.0)
wheel (0.30.0)
请帮忙
编辑: 代码(但也在链接中)
import matplotlib #added this from matplotlib and compiled
matplotlib.use('PS') # generate postscript output by default
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D, get_test_data
from matplotlib import cm
import numpy as np
# set up a figure twice as wide as it is tall
fig = plt.figure(figsize=plt.figaspect(0.5))
#===============
# First subplot
#===============
# set up the axes for the first plot
ax = fig.add_subplot(1, 2, 1, projection='3d')
# plot a 3D surface like in the example mplot3d/surface3d_demo
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
ax.set_zlim(-1.01, 1.01)
fig.colorbar(surf, shrink=0.5, aspect=10)
#===============
# Second subplot
#===============
# set up the axes for the second plot
ax = fig.add_subplot(1, 2, 2, projection='3d')
# plot a 3D wireframe like in the example mplot3d/wire3d_demo
X, Y, Z = get_test_data(0.05)
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
plt.show()
【问题讨论】:
-
你在 jupyter notebook 中使用它吗?
-
可能最好显示您用来尝试绘图的代码?
-
我在 bash shell 中运行并在那里编译,而我在使用 virtualenv 创建的虚拟目录中
-
嘿,我认为我的问题与这张票 github.com/pypa/virtualenv/issues/54 有关。我还阅读了 matplotlib 手册,并说如果我可以使用 venv 就避免使用 virtualenv。我会试一试,如果解决了就会回复你们
-
"PS"后端不显示任何图表。它可用于将图形保存到 ps 文件(因此得名)。
标签: python matplotlib virtualenv mplot3d