简体   繁体   中英

Scatter plot legend shows only one variable with color -Pandas Seaborn

I am trying to plot a scatter plot in pandas with seaborn package. 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. 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:

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>

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM