[英]Scatter plot legend shows only one variable with color -Pandas Seaborn
I am trying to plot a scatter
plot in pandas
with seaborn
package. 我试图用seaborn
包装在pandas
上绘制scatter
。 I want both of my variables to show in legend, but I am only getting one. 我希望我的两个变量都显示在图例中,但我只能得到一个。 Following is a what I did: 以下是我所做的:
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns
df = pd.DataFrame(np.random.randn(100, 6), columns=['a', 'b', 'c', 'd', 'e', 'f'])
I plotted like this, 我这样画
plt.scatter(df['a'],df['b'], color = ['red', 'blue'])
plt.legend(loc = 'best')
plt.show()
I am getting image like this, 我正在得到这样的图像,
As you can see, I do not see a
column/object with blue color. 如您所见,我看不到蓝色a
列/对象。 What mistake am I making here? 我在这里犯什么错误? I searched on this, no luck yet. 我搜索了这个,还没有运气。 Any suggestion would be appreciated. 任何建议,将不胜感激。
UPDATE: 更新:
plt.scatter(df.index, df.a, color='red')
plt.scatter(df.index, df.b, color='blue')
plt.legend(loc = 'best')
Result: 结果:
I think you are confused. 我觉得你很困惑。
Demo: 演示:
Imagine you have only three rows in your DF: 假设您的DF中只有三行:
In [53]: df = pd.DataFrame({'x':[1,2,3], 'y':[5,6,7]})
In [55]: df
Out[55]:
x y
0 1 5
1 2 6
2 3 7
You are trying to plot the following three points - what kind of legend do you expect? 您正在尝试绘制以下三点 -你希望什么样的传奇?
In [54]: plt.scatter(df.x, df.y, color=['red','blue'])
Out[54]: <matplotlib.collections.PathCollection at 0x149f9f28>
In [56]: plt.legend(loc = 'best')
Out[56]: <matplotlib.legend.Legend at 0x1353ca20>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.