我正在测试我在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()
你的代码对我来说很合适。这里有几个3D绘图样本,可以帮助您使用这些类型的东西。
ax = plt.axes(projection='3d')
# Data for a three-dimensional line
zline = np.linspace(0, 15, 1000)
xline = np.sin(zline)
yline = np.cos(zline)
ax.plot3D(xline, yline, zline, 'gray')
# Data for three-dimensional scattered points
zdata = 15 * np.random.random(100)
xdata = np.sin(zdata) + 0.1 * np.random.randn(100)
ydata = np.cos(zdata) + 0.1 * np.random.randn(100)
ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap='Greens');
ax = plt.axes(projection='3d')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
cmap='viridis', edgecolor='none')
ax.set_title('surface');