每个数据点具有不同文本的散点图

问题描述 投票:0回答:9

我正在尝试制作散点图并用列表中的不同数字注释数据点。 因此,例如,我想绘制

y
x
的图,并用
n
中的相应数字进行注释。

y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
x = [0.15, 0.3, 0.45, 0.6, 0.75]
n = [58, 651, 393, 203, 123]
ax = fig.add_subplot(111)
ax1.scatter(z, y, fmt='o')

有什么想法吗?

python matplotlib text scatter-plot annotate
9个回答
798
投票

我不知道有任何采用数组或列表的绘图方法,但您可以在迭代

annotate()
中的值时使用
n

import matplotlib.pyplot as plt
x = [0.15, 0.3, 0.45, 0.6, 0.75]
y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
n = [58, 651, 393, 203, 123]

fig, ax = plt.subplots()
ax.scatter(x, y)

for i, txt in enumerate(n):
    ax.annotate(txt, (x[i], y[i]))

annotate()
有很多格式化选项,请参阅matplotlib网站:

enter image description here


66
投票

如果有人尝试将上述解决方案应用于

.scatter()
而不是
.subplot()

我尝试运行以下代码

import matplotlib.pyplot as plt
x = [0.15, 0.3, 0.45, 0.6, 0.75]
y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
n = [58, 651, 393, 203, 123]

fig, ax = plt.scatter(x, y)

for i, txt in enumerate(n):
    ax.annotate(txt, (x[i], y[i]))

但是遇到了错误,指出“无法解压不可迭代的 PathCollection 对象”,该错误特别指向代码线Fig, ax = plt.scatter(x, y)

我最终使用以下代码解决了该错误

import matplotlib.pyplot as plt
plt.scatter(x, y)

for i, txt in enumerate(n):
    plt.annotate(txt, (x[i], y[i]))

没想到

.scatter()
.subplot()
有区别 我应该更清楚。


45
投票

在 matplotlib 2.0 之前的版本中,无需使用

ax.scatter
来绘制没有标记的文本。在 2.0 版本中,您需要
ax.scatter
来设置适当的文本范围和标记。

import matplotlib.pyplot as plt
y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
z = [0.15, 0.3, 0.45, 0.6, 0.75]
n = [58, 651, 393, 203, 123]

fig, ax = plt.subplots()

for i, txt in enumerate(n):
    ax.annotate(txt, (z[i], y[i]))

在此链接中,您可以找到 3d 示例。


35
投票

您也可以使用

pyplot.text
(请参阅此处)。

def plot_embeddings(M_reduced, word2Ind, words):
    """ 
        Plot in a scatterplot the embeddings of the words specified in the list "words".
        Include a label next to each point.
    """
    for word in words:
        x, y = M_reduced[word2Ind[word]]
        plt.scatter(x, y, marker='x', color='red')
        plt.text(x+.03, y+.03, word, fontsize=9)
    plt.show()

M_reduced_plot_test = np.array([[1, 1], [-1, -1], [1, -1], [-1, 1], [0, 0]])
word2Ind_plot_test = {'test1': 0, 'test2': 1, 'test3': 2, 'test4': 3, 'test5': 4}
words = ['test1', 'test2', 'test3', 'test4', 'test5']
plot_embeddings(M_reduced_plot_test, word2Ind_plot_test, words)


20
投票

我想补充一点,您甚至可以使用箭头/文本框来注释标签。这就是我的意思:

import random
import matplotlib.pyplot as plt


y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
z = [0.15, 0.3, 0.45, 0.6, 0.75]
n = [58, 651, 393, 203, 123]

fig, ax = plt.subplots()
ax.scatter(z, y)

ax.annotate(n[0], (z[0], y[0]), xytext=(z[0]+0.05, y[0]+0.3), 
    arrowprops=dict(facecolor='red', shrink=0.05))

ax.annotate(n[1], (z[1], y[1]), xytext=(z[1]-0.05, y[1]-0.3), 
    arrowprops = dict(  arrowstyle="->",
                        connectionstyle="angle3,angleA=0,angleB=-90"))

ax.annotate(n[2], (z[2], y[2]), xytext=(z[2]-0.05, y[2]-0.3), 
    arrowprops = dict(arrowstyle="wedge,tail_width=0.5", alpha=0.1))

ax.annotate(n[3], (z[3], y[3]), xytext=(z[3]+0.05, y[3]-0.2), 
    arrowprops = dict(arrowstyle="fancy"))

ax.annotate(n[4], (z[4], y[4]), xytext=(z[4]-0.1, y[4]-0.2),
    bbox=dict(boxstyle="round", alpha=0.1), 
    arrowprops = dict(arrowstyle="simple"))

plt.show()

这将生成以下图表:


16
投票

对于有限的值集,matplotlib 就可以了。但是,当您有很多值时,工具提示开始与其他数据点重叠。但由于空间有限,您不能忽略这些值。因此最好缩小或放大。

使用情节

import plotly.express as px
import pandas as pd

df = px.data.tips()

df = px.data.gapminder().query("year==2007 and continent=='Americas'")


fig = px.scatter(df, x="gdpPercap", y="lifeExp", text="country", log_x=True, size_max=100, color="lifeExp")
fig.update_traces(textposition='top center')
fig.update_layout(title_text='Life Expectency', title_x=0.5)
fig.show()


13
投票

Python 3.6+:

coordinates = [('a',1,2), ('b',3,4), ('c',5,6)]
for x in coordinates: plt.annotate(x[0], (x[1], x[2]))

4
投票

当您需要在不同时间单独注释时(我的意思是,不是在单个 for 循环中),这可能很有用

ax = plt.gca()
ax.annotate('your_lable', (x,y)) 

其中

x
y
是您的 目标坐标,类型为 float/int。


3
投票

作为使用列表理解和 numpy 的单行代码:

[ax.annotate(x[0], (x[1], x[2])) for x in np.array([n,z,y]).T]

设置与 Rutger 的答案相同。

© www.soinside.com 2019 - 2024. All rights reserved.