当我在matplotlib中绘制线段时,线宽似乎被添加到线的长度。在我的代码下面(不是最pythonic代码,但它应该做的伎俩)。我做错了还是这只是matplotlib的一个功能?
import matplotlib.pyplot as plt
import numpy as np
L1 = 100
L2 = 75
L3 = 100
Y = 3
N = 5
l_prev = 0
for l, c in zip(np.linspace(0, L1, N), range(N)):
plt.plot([l_prev, l], [0, 0], 'r', linewidth=20)
l_prev = l
l_prev = L1
for l, c in zip(np.linspace(L1, L1 + L2, N), range(N)):
plt.plot([l_prev, l], [Y, Y], 'g', linewidth=1)
l_prev = l
l_prev = L1
for l, c in zip(np.linspace(L1, L1 + L3, N), range(N)):
p = plt.plot([l_prev, l], [-Y, -Y], 'b', linewidth=10)
l_prev = l
plt.axvspan(xmin=L1, xmax=L1)
plt.axis([-5, 205, -5, 5])
plt.show()
我期望看到的是三个线段:[0,L1], [L1,L2] and [L1,L3]
。但第一行[0,L1]
延伸到L1
+'直径'....
答案 0 :(得分:55)
默认solid_capstyle
似乎是projecting
,这不是您想要的那个:
plt.figure()
plt.plot([0, 100], [5, 5], linewidth=50, linestyle="-", c="blue",
solid_capstyle="butt")
plt.plot([0, 100], [15, 15], linewidth=50, linestyle="-", c="red",
solid_capstyle="round")
plt.plot([0, 100], [25, 25], linewidth=50, linestyle="-", c="purple",
solid_capstyle="projecting")
plt.axvline(x=100, c="black")
plt.xlim(0, 125)
plt.ylim(0, 30)
plt.savefig("cap.png")