简体   繁体   中英

How can I change the python matplotlib.pyplot legend marker into a serial number like 1,2,3 instead of shape or character?

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.

You could use a generator (eg, 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:

示例图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM