2012-04-24 17 views
22

Cuando dibujo un segmento de línea en matplotlib, el ancho de línea parece agregarse a la longitud de la línea. Debajo de mi código (no es el código más pitónico, pero debería ser el truco). ¿Estoy haciendo algo mal o es solo una característica de matplotlib?Matplotlib: el ancho de línea se agrega a la longitud de una línea

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() 

Lo que esperaba ver es tres segmentos de línea: [0,L1], [L1,L2] and [L1,L3]. Pero la primera línea [0,L1] extiende a L1 + 'el diámetro' ....

Respuesta

50

Parece que el valor por defecto es solid_capstyleprojecting, que no es el que desea:

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") 

enter image description here

+0

Estupendo. ¡Exactamente lo que quería! – user989762

+0

+1 no tenía conocimiento de 'solid_capstyle' – bmu

Cuestiones relacionadas