Python scatter-plot: Conditions for marker styles?

大城市里の小女人 提交于 2019-12-10 20:46:28

问题


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...


回答1:


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


来源:https://stackoverflow.com/questions/16774586/python-scatter-plot-conditions-for-marker-styles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!