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.

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

7 Comments

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 oops sorry, but how do you want to iterate over the values? random? manual custom order?
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
Sorry but not clear to me, markers are not numbers, can you provide a schematic?
Yes, as markers are not numbers can I change those into numbers? Like > 1 RED CROSS 2. GREEN LINE 3 RED DOT
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.