pandas 数据框中值的散点图 [英] Scatter plot of values in pandas dataframe

查看:39
本文介绍了 pandas 数据框中值的散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a pandas dataframe in the following format. I am trying to plot this data based on ClusterAssigned, with probably different colors for 0 and 1.

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

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

I tried with this code but does not seem to yield perfect results.

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()

推荐答案

需要使用 matplotlib 中的 scatter 函数,无需循环或分组.

You need to use the scatter function in matplotlib and there is no need to loop or do any grouping.

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)

这篇关于 pandas 数据框中值的散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆