Python散点图:标记样式的条件? [英] Python scatter-plot: Conditions for marker styles?

查看:47
本文介绍了Python散点图:标记样式的条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,我希望用 matplotlib 绘制为散点图,以及一个大小相同的向量,用于对数据点进行分类和标记(离散地,例如从 0 到 3).我想对不同的标签使用不同的标记(例如 'x' 代表 0,'o' 代表 1 等等).我该如何优雅地解决这个问题?我很确定我只是错过了一些东西,但并没有真正找到它,而且我的幼稚方法到目前为止都失败了......

I have a data set I wish to plot as scatter plot with matplotlib, and a vector the same size that categorizes and labels the data points (discretely, e.g. from 0 to 3). I want to use different markers for different labels (e.g. 'x' for 0, 'o' for 1 and so on). How can I solve this elegantly? I am quite sure I am just missing out on something, but didn't really find it, and my naive approaches failed so far...

推荐答案

如何遍历所有这样的标记:

What about iterating over all markers like this:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.rand(100)
y = np.random.rand(100)
category = np.random.random_integers(0, 3, 100)

markers = ['s', 'o', 'h', '+']
for k, m in enumerate(markers):
    i = (category == k)
    plt.scatter(x[i], y[i], marker=m)

plt.show()

这篇关于Python散点图:标记样式的条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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