[英]Pandas Matplotlib: How to change shape and size of the legend in scatter plot?
[英]How to draw legend for scatter plot indicating size?
我在matplotlib
有一個散點圖。 散點圖顯示的圓具有不同的顏色陰影以及不同的大小。
我只想為圓的大小添加一個圖例。 圖例不應該用情節中的圓環的顏色來着色,而應該說“灰色”。 然后應該有三個條目,分別對應於一個大灰色圓圈,一個中灰色圓圈和一個小灰色圓圈,全部帶有一些文本。 在一個圖例條目中,我只需要一個圓圈,而不是matplotlib中默認的多個圓圈。
我已經根據http://matplotlib.org/users/legend_guide.html進行了嘗試,
但它給出一個矩形,而不是一個圓形。
red_patch = mpatches.Circle((3,3), radius = 1000, color='blue', label='The red data')
ax1.legend(handles=[red_patch])
自動執行盡可能多的工作:
import matplotlib.pyplot as plt
from numpy.random import random
colors = ['b', 'c', 'y', 'm', 'r']
ll = plt.scatter(random(10), random(10), s=random(10)*10, marker='o', color=colors[0])
l = plt.scatter(random(10), random(10), s=random(10)*20, marker='o', color=colors[1])
a = plt.scatter(random(10), random(10), s = random(10)*300, marker='o', color=colors[2])
z = plt.scatter(random(10), random(10), s = 35, marker='+', color=colors[3]) # not in legend
gll = plt.scatter([],[], s=10, marker='o', color='#555555')
gl = plt.scatter([],[], s=20, marker='o', color='#555555')
ga = plt.scatter([],[], s=300, marker='o', color='#555555')
plt.legend((gll,gl,ga),
('10', '20', '300'),
scatterpoints=1,
loc='lower left',
ncol=1,
fontsize=8)
plt.show()
可以通過size sizes=[10,20,300]
類的循環來解決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.