Matplotlib 线条连接不流畅,Python
Matplotlib lines do not join smoothly, Python
我正在使用 matplotlib 绘制圆柱体的轮廓,但是线条不想平滑地连接起来,如 x[40,60] 范围内所示。
我知道这张图片非常微妙,但不幸的是,它不符合我的目的。我希望你能看到它。
使用更多的数据点似乎没有什么不同。
有没有办法让曲线在matplotlib中更平滑地连接起来?
原代码:
import numpy as np
import matplotlib.pylab as plt
length = 100.
a = 40
b = 20
n = 2.
alpha = np.radians(25.)
d = 18.
x_nose = np.linspace(0,a,1000)
r_nose = (0.5*d*(1 - ((x_nose-a)/a)**2)**(1/n))
x_mid = np.linspace(x_nose[-1],a+b,2)
r_mid = np.array([r_nose[-1],r_nose[-1]])
x_tail = np.linspace(x_mid[-1],length,1000)
l_tail = length-a-b
r_tail = (0.5*d - ((3*d)/(2*l_tail**2) - np.tan(alpha)/l_tail)*(x_tail-a-b)**2 + (d/l_tail**3 - np.tan(alpha)/l_tail**2)*(x_tail-a-b)**3)
fig = plt.figure()
plt.plot(x_nose,r_nose,'k',linewidth=2,antialiased=True)
plt.plot(x_mid,r_mid,'k',linewidth=2,antialiased=True)
plt.plot(x_tail,r_tail,'k',linewidth=2,antialiased=True)
plt.axis('equal')
plt.show()
放大后更容易看到效果:
我不确定为什么会发生这种情况,但您可以通过构建一个 x
和 r
数组来缓解这种情况,并绘制整条线。
x = np.append(x_nose, x_mid)
x = np.append(x, x_tail )
r = np.append(r_nose, r_mid)
r = np.append(r, r_tail )
plt.plot(x,r,'k',linewidth=2,antialiased=True)
这显然可以防止您更改单个元素的线条样式,但看起来您不想那样做。这对我有用:
我正在使用 matplotlib 绘制圆柱体的轮廓,但是线条不想平滑地连接起来,如 x[40,60] 范围内所示。
我知道这张图片非常微妙,但不幸的是,它不符合我的目的。我希望你能看到它。
使用更多的数据点似乎没有什么不同。
有没有办法让曲线在matplotlib中更平滑地连接起来?
原代码:
import numpy as np
import matplotlib.pylab as plt
length = 100.
a = 40
b = 20
n = 2.
alpha = np.radians(25.)
d = 18.
x_nose = np.linspace(0,a,1000)
r_nose = (0.5*d*(1 - ((x_nose-a)/a)**2)**(1/n))
x_mid = np.linspace(x_nose[-1],a+b,2)
r_mid = np.array([r_nose[-1],r_nose[-1]])
x_tail = np.linspace(x_mid[-1],length,1000)
l_tail = length-a-b
r_tail = (0.5*d - ((3*d)/(2*l_tail**2) - np.tan(alpha)/l_tail)*(x_tail-a-b)**2 + (d/l_tail**3 - np.tan(alpha)/l_tail**2)*(x_tail-a-b)**3)
fig = plt.figure()
plt.plot(x_nose,r_nose,'k',linewidth=2,antialiased=True)
plt.plot(x_mid,r_mid,'k',linewidth=2,antialiased=True)
plt.plot(x_tail,r_tail,'k',linewidth=2,antialiased=True)
plt.axis('equal')
plt.show()
放大后更容易看到效果:
我不确定为什么会发生这种情况,但您可以通过构建一个 x
和 r
数组来缓解这种情况,并绘制整条线。
x = np.append(x_nose, x_mid)
x = np.append(x, x_tail )
r = np.append(r_nose, r_mid)
r = np.append(r, r_tail )
plt.plot(x,r,'k',linewidth=2,antialiased=True)
这显然可以防止您更改单个元素的线条样式,但看起来您不想那样做。这对我有用: