【问题标题】:Scatter plot of values in pandas dataframe熊猫数据框中值的散点图
【发布时间】:2017-05-07 08:45:42
【问题描述】:

我有一个以下格式的熊猫数据框。我正在尝试根据 ClusterAssigned 绘制此数据,0 和 1 的颜色可能不同。

    Distance    ClusterAssigned
    23      1
    35      1
    20      1
    264     0
    830     0

我尝试使用此代码,但似乎没有产生完美的结果。

groups = dfprintscatter.groupby('ClusterAssigned')

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.margins(0.05) 
for name, group in groups:
        ax.plot(group.Distance, group.ClusterAssigned, marker='o', linestyle='', ms=5, label=name)
ax.legend()

plt.show()

【问题讨论】:

  • 你认为完美的结果是什么?

标签: python pandas plot


【解决方案1】:

你需要在matplotlib中使用scatter函数,不需要循环或者做任何分组。

x = np.arange(len(dfprintscatter))
y = dfprintscatter.Distance
c = dfprintscatter.ClusterAssigned
plt.scatter(x, y, c=c, marker='o') 

使用seaborn

import seaborn as sns
sns.lmplot(x=np.arange(len(dfprintscatter)), y='Distance', hue='ClusterAssigned', fit_reg=False)

【讨论】:

  • 谢谢,这就是我真正想要的。我可以在 plt.scatter 中分配标签吗?
  • 您必须循环以获取正确的标签。使用 seaborn 会容易得多。
  • 我尝试使用 seaborn,我得到错误“regplot() got an unexpected keyword argument 'hue'”
  • @user3447653 我的错。 Regplot 没有色调参数。用 'lmplot' 试试同样的方法
猜你喜欢
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-16
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
相关资源
最近更新 更多