簡體   English   中英

如何將 python matplotlib.pyplot 圖例標記更改為序列號,如 1、2、3,而不是形狀或字符?

[英]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()

在上面的代碼中,圖例標記是“r+”、“go--”和“ro”,但我希望它變成 1,2 和 3,因為有 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()

output:

示例圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM