Reputation: 3046
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()
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.
Upvotes: 1
Views: 2788
Reputation: 210812
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>
Upvotes: 3