Py学习  »  Python

如何更改python matplotlib。pyplot将图例标记转换为序列号,如1,2,3,而不是形状或字符?

daspran • 3 年前 • 1337 次点击  
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()

在上面的代码中,图例标记是“r+”、“go-”和“ro”,但我希望它变成1、2和3,因为有3个图。?有人能帮我解决这个问题吗?还有什么方法可以不用硬编码就能实现吗?""" 非常感谢。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/128490
 
1337 次点击  
文章 [ 1 ]  |  最新文章 3 年前
mozway
Reply   •   1 楼
mozway    3 年前

你可以使用发电机(例如:。, itertools.count )及 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()

输出:

example plot