1
import matplotlib.pyplot

plt.figure() 
plt.plot(x, 'r+', label='one')
plt.plot(x1, 'go--', label ='two')
plt.plot(y, 'ro', label='Three')
plt.legend()

In the above code legend marker is 'r+' , 'go--' and 'ro' but I want it to change into 1,2 and 3 as there are 3 plots.? Can anyone help me in solving this issue? Also is there any anyway it can be done without hardcoding the numbers? """ Thank you.

daspran
  • 11
  • 1

1 Answers1

0

You could use a generator (e.g., itertools.count) and next:

import matplotlib.pyplot

x=x1=y=(0,0) # dummy data

markers = iter(['r+', 'go--', 'ro'])

plt.figure() 
plt.plot(x, next(markers), label='1')
plt.plot(x1, next(markers), label='2')
plt.plot(y, next(markers), label='3')
plt.legend()

output:

example plot

mozway
  • 194,879
  • 13
  • 39
  • 75
  • Actually I am trying to change the marker of the legend, i.e. the red plus sign, green one and the red dot. Not the labels of the legend. – daspran Oct 03 '21 at 17:11
  • @daspran oops sorry, but how do you want to iterate over the values? random? manual custom order? – mozway Oct 03 '21 at 17:19
  • As per the plotting sequence. Suppose I plot a bar char with 3 different variables then the 1st marker will be 1st bar. 2nd marker will be 2nd bar and so on. Instead of the color of the bars I want it numbered sequentially as plotted. How can I do it? any suggestion please – daspran Oct 03 '21 at 17:26
  • Sorry but not clear to me, markers are not numbers, can you provide a schematic? – mozway Oct 03 '21 at 17:27
  • Yes, as markers are not numbers can I change those into numbers? Like > 1 RED CROSS 2. GREEN LINE 3 RED DOT – daspran Oct 03 '21 at 17:30
  • @daspran check my updated answer, still not sure if this is what you want :p – mozway Oct 03 '21 at 17:33
  • other option use a dictionary with numbers as keys and 'r+', etc as values – mozway Oct 03 '21 at 17:34