问题描述
我有以下脚本:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
nn = 400 # number of points along circle's perimeter
theta = np.linspace(0,2*np.pi,nn)
rho = np.ones(nn)
# (x,y) represents points on circle's perimeter
x = np.ravel(rho*np.cos(theta))
y = np.ravel(rho*np.sin(theta))
fig,ax = plt.subplots()
plt.rcParams["figure.figsize"] = [6,10]
ax = plt.axes(projection='3d') # set the axes for 3D plot
ax.azim = -90 # y rotation (default=270)
ax.elev = 21 # x rotation (default=0)
# low,high values of z for plotting 2 circles at different elev.
loz,hiz = -15,15
# Plot two circles
ax.plot(x,y,hiz)
ax.plot(x,loz)
# set some indices to get proper (x,y) for line plotting
lo1,hi1 = 15,15+nn//2
lo2,hi2 = lo1+nn//2-27,hi1-nn//2-27
# plot 3d lines using coordinates of selected points
ax.plot([x[lo1],x[hi1]],[y[lo1],y[hi1]],[loz,hiz])
ax.plot([x[lo2],x[hi2]],[y[lo2],y[hi2]],hiz])
ax.plot([0,0],[0,10])
ax.plot([0,[9,0])
ax.plot([0,8,0])
plt.show()
在脚本的最后,我想在三个方向上绘制三行。怎么做?为什么这样:
ax.plot([0,0])
将线对准同一方向?
请问第二个问题。如何使圆锥更窄(底面更类似于圆形)?
现在输出:
解决方法
注释完代码的最后三行后,图像就是我得到的输出
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
nn = 400 # number of points along circle's perimeter
theta = np.linspace(0,2*np.pi,nn)
rho = np.ones(nn)
# (x,y) represents points on circle's perimeter
x = np.ravel(rho*np.cos(theta))
y = np.ravel(rho*np.sin(theta))
fig,ax = plt.subplots()
plt.rcParams["figure.figsize"] = [6,10]
ax = plt.axes(projection='3d') # set the axes for 3D plot
ax.azim = -90 # y rotation (default=270)
ax.elev = 21 # x rotation (default=0)
# low,high values of z for plotting 2 circles at different elev.
loz,hiz = -15,15
# Plot two circles
ax.plot(x,y,hiz)
ax.plot(x,loz)
# set some indices to get proper (x,y) for line plotting
lo1,hi1 = 15,15+nn//2
lo2,hi2 = lo1+nn//2-27,hi1-nn//2-27
# plot 3d lines using coordinates of selected points
ax.plot([x[lo1],x[hi1]],[y[lo1],y[hi1]],[loz,hiz])
ax.plot([x[lo2],x[hi2]],[y[lo2],y[hi2]],hiz])
#ax.plot([0,0],[0,10])
#ax.plot([0,[9,0])
#ax.plot([0,8,0])
plt.show()
您可以看到基座几乎是一个完美的圆。因为您还在图形中绘制线,所以给人一种幻觉,即基数不是圆。
关于3个不同方向的线。由于这部分代码
ax.plot([0,10])
ax.plot([0,0])
ax.plot([0,0])
在X轴上全为零,实际上只是在Y轴上绘制线。
当我在X轴部分中输入一些值时,就像这样
ax.plot([1,5],0])
输出为
我希望这就是你的要求。
, ax.plot([0,10])
给出plot
的3点的x和y坐标,但是您没有在z方向上给出任何坐标。请记住,plot
的输入是x,y,z,而不是您似乎假定的(x0,y0,z0),(x1,y1,z1)
因此,这绘制了3条“线”,其中两条在x = y = z = 0处开始和结束,其中一条延伸到y = 10。您遇到的另外两个ax.plot
呼叫正在做类似的事情。
要绘制从原点开始并沿x,y或z方向之一延伸的三条线,您可能打算使用:
ax.plot([0,10]) # extend in z direction
ax.plot([0,8],0]) # extend in y direction
ax.plot([0,9],0]) # extend in x direction
请注意,这也使您的圈子看起来更像圈子