散点图图例仅显示一个颜色为 -Pandas Seaborn 的变量
Scatter plot legend shows only one variable with color -Pandas Seaborn
我正在尝试使用 seaborn
包在 pandas
中绘制 scatter
图。我希望我的两个变量都显示在图例中,但我只得到一个。以下是我所做的:
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'])
我是这样画的,
plt.scatter(df['a'],df['b'], color = ['red', 'blue'])
plt.legend(loc = 'best')
plt.show()
我得到这样的图像,
如您所见,我没有看到 a
column/object 是蓝色的。我在这里犯了什么错误?我搜索了这个,还没有运气。如有任何建议,我们将不胜感激。
更新:
plt.scatter(df.index, df.a, color='red')
plt.scatter(df.index, df.b, color='blue')
plt.legend(loc = 'best')
结果:
我觉得你很困惑。
演示:
假设您的 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
您正在尝试绘制以下 三个 点 - 您期望什么样的图例?
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>
我正在尝试使用 seaborn
包在 pandas
中绘制 scatter
图。我希望我的两个变量都显示在图例中,但我只得到一个。以下是我所做的:
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'])
我是这样画的,
plt.scatter(df['a'],df['b'], color = ['red', 'blue'])
plt.legend(loc = 'best')
plt.show()
我得到这样的图像,
如您所见,我没有看到 a
column/object 是蓝色的。我在这里犯了什么错误?我搜索了这个,还没有运气。如有任何建议,我们将不胜感激。
更新:
plt.scatter(df.index, df.a, color='red')
plt.scatter(df.index, df.b, color='blue')
plt.legend(loc = 'best')
结果:
我觉得你很困惑。
演示:
假设您的 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
您正在尝试绘制以下 三个 点 - 您期望什么样的图例?
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>