Marco
Marco

Reputation: 231

Matplotlib - Line connecting points incorrectly

I am plotting via matplotlib/pyplot. Why the initial points (blue) of star and circle are connecting a line to the final points? Initial point star is connecting the final point circle, and the initial point circle is connecting the final point square.

This is the source code:

plt.plot(x,y, color="black")
plt.errorbar(x1,y1,yerr=err1,marker='s', markersize="10", mfc='blue', color='blue',fmt = '')
plt.errorbar(x2,y2,yerr=err2,marker='s', markersize="10", mfc='green', color='green',fmt = '')
plt.errorbar(x3,y3,yerr=err3,marker='s', markersize="10", mfc='red', color='red',fmt = '')
plt.errorbar(x4,y4,yerr=err4,marker='s', markersize="10", mfc='yellow', color='yellow',fmt = '')
plt.errorbar(x5,y5,yerr=err5,marker='s', markersize="10", mfc='orange', color='orange',fmt = '')
plt.errorbar(x6,y6,yerr=err6,marker='s', markersize="10", mfc='purple', color='purple',fmt = '')
plt.errorbar(x7,y7,yerr=err7,marker='s', markersize="10", mfc='magenta', color='magenta',fmt = '')
plt.errorbar(x8,y8,yerr=err8,marker='s', markersize="10", mfc='cyan', color='cyan',fmt = '')
plt.errorbar(x9,y9,yerr=err9,marker='o', markersize="10", mfc='blue', color='blue',fmt = '')
plt.errorbar(x10,y10,yerr=err10,marker='o', markersize="10", mfc='green', color='green',fmt = '')
plt.errorbar(x11,y11,yerr=err11,marker='o', markersize="10", mfc='red', color='red',fmt = '')
plt.errorbar(x12,y12,yerr=err12,marker='o', markersize="10", mfc='yellow', color='yellow',fmt = '')
plt.errorbar(x13,y13,yerr=err13,marker='o', markersize="10", mfc='orange', color='orange',fmt = '')
plt.errorbar(x14,y14,yerr=err14,marker='o', markersize="10", mfc='purple', color='purple',fmt = '')
plt.errorbar(x15,y15,yerr=err15,marker='o', markersize="10", mfc='magenta', color='magenta',fmt = '')
plt.errorbar(x16,y16,yerr=err16,marker='o', markersize="10", mfc='cyan', color='cyan',fmt = '')
plt.errorbar(x17,y17,yerr=err17,marker='*', markersize="10", mfc='blue', color='blue',fmt = '')
plt.errorbar(x18,y18,yerr=err18,marker='*', markersize="10", mfc='green', color='green',fmt = '')
plt.errorbar(x19,y19,yerr=err19,marker='*', markersize="10", mfc='red', color='red',fmt = '')
plt.errorbar(x20,y20,yerr=err20,marker='*', markersize="10", mfc='yellow', color='yellow',fmt = '')
plt.errorbar(x21,y21,yerr=err21,marker='*', markersize="10", mfc='orange', color='orange',fmt = '')
plt.errorbar(x22,y22,yerr=err22,marker='*', markersize="10", mfc='purple', color='purple',fmt = '')
plt.errorbar(x23,y23,yerr=err23,marker='*', markersize="10", mfc='magenta', color='magenta',fmt = '')
plt.errorbar(x24,y24,yerr=err24,marker='*', markersize="10", mfc='cyan',   color='cyan',fmt = '')
plt.savefig("image.png",bbox_inches='tight')
plt.show()

"x" variable is a shape 24 array. "y" variable is a shape 24 array "err" variables, are just real numbers.

enter image description here

Upvotes: 1

Views: 418

Answers (1)

Thomas Kühn
Thomas Kühn

Reputation: 9810

If you want to plot three separate lines of eight points each, you cannot plot them 'at once' using two arrays of length 24. Instead, split your data into three separate peaces and plot them separately:

plot(x[0:8],y[0:8],'black')
plot(x[8:16],y[8:16],'black')
plot(x[16:24],y[16:24],'black')

This should do the trick. Note also, that you can shorten your errorbar code considerably by using lists and for statements. For instance you could do

errs = [err1,err2,err3,err4,err5,err6,err7,err8] #probably you did have these in a list or array anyway
cols = ['b','g','r','y','orange','purple','m','c']

for i,(errval,col) in enumerate(zip(errs,cols)): 
     plt.errorbar(x[i],y[i],yerr=errvall, marker='s', markersize="10", mfc=col, color=col,fmt = '')

with another for loop you could still loop through the markers. Hope this helps.

Upvotes: 1

Related Questions