簡體   English   中英

在每個數據點使用不同的文本散布 plot

[英]Scatter plot with different text at each data point

我正在嘗試制作散點圖 plot 並用列表中的不同數字注釋數據點。 因此,例如,我想 plot y vs x並使用n中的相應數字進行注釋。

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]
ax = fig.add_subplot(111)
ax1.scatter(z, y, fmt='o')

有任何想法嗎?

我不知道任何采用數組或列表的繪圖方法,但您可以在迭代n中的值時使用annotate()

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)

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

annotate()有很多格式化選項,請參閱matplotlib 網站:

在此處輸入圖像描述

如果有人試圖將上述解決方案應用於.scatter()而不是.subplot()

我嘗試運行以下代碼

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.scatter(z, y)

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

但是遇到了“無法解壓縮不可迭代的 PathCollection 對象”的錯誤,該錯誤專門指向代碼行 fig, ax = plt.scatter(z, y)

我最終使用以下代碼解決了錯誤

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

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

我沒想到.scatter().subplot()我應該知道的更好。

在 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 中的示例。

你也可以使用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)

在此處輸入圖像描述

我想補充一點,您甚至可以使用箭頭/文本框來注釋標簽。 這就是我的意思:

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

這將生成以下圖表: 在此處輸入圖像描述

對於有限的一組值,matplotlib 很好。 但是,當您有很多值時,工具提示開始與其他數據點重疊。 但是由於空間有限,您不能忽略這些值。 因此,最好縮小或放大。

使用情節

import plotly.express as px
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()

在此處輸入圖像描述

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

作為使用列表理解和 numpy 的單行程序:

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

設置與羅格的回答同上。

當您需要在不同時間單獨注釋時,這可能很有用(我的意思是,不是在單個 for 循環中)

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

其中xy是您的目標坐標,類型是浮點數/整數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM