有没有一种方法可以向散点图的大小与某些数据成比例的散点图添加辅助图例?
我写了下面的代码来生成散点图。散点图的颜色表示年份(取自用户定义的df),散点图的大小表示变量3(也取自df,但为原始数据):
import pandas as pd
colors = pd.DataFrame({'1985':'red','1990':'b','1995':'k','2000':'g','2005':'m','2010':'y'}, index=[0,1,2,3,4,5])
fig = plt.figure()
ax = fig.add_subplot(111)
for i in df.keys():
df[i].plot(kind='scatter',x='variable1',y='variable2',ax=ax,label=i,s=df[i]['variable3']/100, c=colors[i])
ax.legend(loc='upper right')
ax.set_xlabel("Variable 1")
ax.set_ylabel("Variable 2")
Run Code Online (Sandbox Code Playgroud)
此代码(包含我的数据)产生以下图形:
因此,虽然对颜色/年份进行了很好的明确定义,但分散的大小却没有。
如何添加辅助或附加图例来定义散点图的大小?