栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Pandas/Pyplot中的散点图:如何按类别绘制

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Pandas/Pyplot中的散点图:如何按类别绘制

你可以使用

scatter
它,但是这需要为你提供数值
key1
,并且你不会注意到图例。

最好只

plot
对像这样的离散类别使用。例如:

import matplotlib.pyplot as pltimport numpy as npimport pandas as pdnp.random.seed(1974)# Generate Datanum = 20x, y = np.random.random((2, num))labels = np.random.choice(['a', 'b', 'c'], num)df = pd.Dataframe(dict(x=x, y=y, label=labels))groups = df.groupby('label')# Plotfig, ax = plt.subplots()ax.margins(0.05) # Optional, just adds 5% padding to the autoscalingfor name, group in groups:    ax.plot(group.x, group.y, marker='o', linestyle='', ms=12, label=name)ax.legend()plt.show()

如果你希望外观看起来像默认pandas样式,则只需rcParams使用pandas样式表进行更新,并使用其颜色生成器即可。(我也略微调整了图例):

import matplotlib.pyplot as pltimport numpy as npimport pandas as pdnp.random.seed(1974)# Generate Datanum = 20x, y = np.random.random((2, num))labels = np.random.choice(['a', 'b', 'c'], num)df = pd.Dataframe(dict(x=x, y=y, label=labels))groups = df.groupby('label')# Plotplt.rcParams.update(pd.tools.plotting.mpl_stylesheet)colors = pd.tools.plotting._get_standard_colors(len(groups), color_type='random')fig, ax = plt.subplots()ax.set_color_cycle(colors)ax.margins(0.05)for name, group in groups:    ax.plot(group.x, group.y, marker='o', linestyle='', ms=12, label=name)ax.legend(numpoints=1, loc='upper left')plt.show()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/431804.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号