简体   繁体   English

散点图标签在一行 - Matplotlib

[英]Scatter plot labels in one line - Matplotlib

It's possible to set all labels at once in Matplolib? 可以在Matplolib中一次设置所有标签吗?

For example, I have this piece of code to plot a scatter plot: 例如,我有这段代码来绘制散点图:

cmap = plt.get_cmap('Set1')
colors = [cmap(i) for i in numpy.linspace(0, 1, simulations+1)]

plt.figure(figsize=(7, 7))
plt.scatter(coords[:, 0], coords[:, 1], marker='o', c=colors, s=50, edgecolor='None')
plt.legend(loc='lower left',)

where simulations = 7 and coords is a numpy.array with shape (7, 2). simulations = 7coords是一个numpy.array形状(7,2)。

This gives me a plot like that: 这给了我一个这样的情节:

在此输入图像描述

If I change the last line for: 如果我更改最后一行:

plt.scatter(coords[:, 0], coords[:, 1], marker='o', c=colors, s=50, edgecolor='None', label=range(simulations))
plt.legend(loc='lower left')

I get: 我明白了:

在此输入图像描述

I'm wondering if I'll have to do a loop to do the scatter and set each label of if there is some way to do all at once. 我想知道我是否必须做一个循环来进行分散并设置每个标签,如果有一些方法可以一次完成所有操作。

Thank you. 谢谢。

I'm not sure how to do it with a scatter plot. 我不知道如何用散点图来做到这一点。 But I'm not sure if there is an advantage to use scatter rather than plot if you want different labels. 但是我不确定如果你想要不同的标签,使用scatter而不是plot是否有优势。

How about this? 这个怎么样?

import numpy as np
import matplotlib.pyplot as plt

n = 10
coords = np.random.random((n,2))

cmap = plt.get_cmap('Set1')

for i, (x, y) in enumerate(coords):
    plt.plot(x, y, 'o', color=cmap(i/float(n)), label='%i'%i, ms=9, mec='none')

plt.axis((-0.5, 1.5, -0.5, 1.5))
plt.legend(loc='lower left', numpoints=1, frameon=False)
plt.show()

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM