Bottom Page

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


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

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Remove internal lines donut plot matplotlib diego_last 5 3,142 Dec-14-2017, 04:05 PM
Last Post: diego_last

Forum Jump:


Users browsing this thread: 1 Guest(s)