Python Forum

Full Version: Matplotlib 2 lines with different markers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I want to create a matplotlib plot for 2 different lines, however I need the lines to have different markers.
i.e. use 'x' as marker for line 1 and '^' as marker for line 2.
I tried to use itertools.next() as I saw in some online suggestions but no luck, plot still appears with 'x' as marker for both lines. Can someone help?
My MWE code and the resulted plot below:

from matplotlib import pyplot as plt
import itertools

xlist = [5, 10, 15, 20, 25]
ylist = [[0.260864, 0.238644], [0.514823, 0.473904], [0.764035, 0.714735], [1.021157, 0.950179], [1.2735940000000001, 1.186469]]

labelList = ["Line1","Line2"] 
fig, ax1 = plt.subplots()
markers = itertools.cycle(['x','^','v'])
ax1.plot(xlist,ylist,linewidth=1,marker=next(markers))
ax1.set_xlabel('x axis')
ax1.set_ylabel('y axis')
plt.legend(labelList) 
plt.show()
[Image: l25X4n4.png]

Solution:
from matplotlib import pyplot as plt
import itertools

xlist = [5, 10, 15, 20, 25]
ylist = [[0.260864, 0.238644], [0.514823, 0.473904], [0.764035, 0.714735], [1.021157, 0.950179], [1.2735940000000001, 1.186469]]

labelList = ["Line1","Line2"] 
fig, ax1 = plt.subplots()
markers = itertools.cycle(['x','^','v'])
ax1.plot(xlist,[item[0] for item in ylist],linewidth=1,marker=next(markers))
ax1.plot(xlist,[item[1] for item in ylist],linewidth=1,marker=next(markers))

ax1.set_xlabel('x axis')
ax1.set_ylabel('y axis')
plt.legend(labelList) 
plt.show()