Python Forum
Matplotlib 2 lines with different markers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matplotlib 2 lines with different markers
#1
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()
Reply


Messages In This Thread
Matplotlib 2 lines with different markers - by medatib531 - Sep-15-2020, 09:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  scatter3D different markers per data erdemath 5 1,694 May-27-2022, 03:55 PM
Last Post: erdemath
  Remove internal lines donut plot matplotlib diego_last 5 6,218 Dec-14-2017, 04:05 PM
Last Post: diego_last

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020