简体   繁体   English

在每个数据点使用不同的文本散布 plot

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

I am trying to make a scatter plot and annotate data points with different numbers from a list.我正在尝试制作散点图 plot 并用列表中的不同数字注释数据点。 So, for example, I want to plot y vs x and annotate with corresponding numbers from n .因此,例如,我想 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')

Any ideas?有任何想法吗?

I'm not aware of any plotting method which takes arrays or lists but you could use annotate() while iterating over the values in n .我不知道任何采用数组或列表的绘图方法,但您可以在迭代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]))

There are a lot of formatting options for annotate() , see the matplotlib website: annotate()有很多格式化选项,请参阅matplotlib 网站:

在此处输入图像描述

In case anyone is trying to apply the above solutions to a .scatter() instead of a .subplot() ,如果有人试图将上述解决方案应用于.scatter()而不是.subplot()

I tried running the following code我尝试运行以下代码

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

But ran into errors stating "cannot unpack non-iterable PathCollection object", with the error specifically pointing at codeline fig, ax = plt.scatter(z, y)但是遇到了“无法解压缩不可迭代的 PathCollection 对象”的错误,该错误专门指向代码行 fig, ax = plt.scatter(z, y)

I eventually solved the error using the following code我最终使用以下代码解决了错误

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

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

I didn't expect there to be a difference between .scatter() and .subplot() I should have known better.我没想到.scatter().subplot()我应该知道的更好。

In versions earlier than matplotlib 2.0, ax.scatter is not necessary to plot text without markers.在 matplotlib 2.0 之前的版本中, ax.scatter来绘制没有标记的文本。 In version 2.0 you'll need ax.scatter to set the proper range and markers for text.在 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]))

And in this link you can find an example in 3d.在此链接中,您可以找到 3d 中的示例。

You may also use pyplot.text (see here ).你也可以使用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)

在此处输入图像描述

I would love to add that you can even use arrows /text boxes to annotate the labels.我想补充一点,您甚至可以使用箭头/文本框来注释标签。 Here is what I mean:这就是我的意思:

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

Which will generate the following graph:这将生成以下图表: 在此处输入图像描述

For limited set of values matplotlib is fine.对于有限的一组值,matplotlib 很好。 But when you have lots of values the tooltip starts to overlap over other data points.但是,当您有很多值时,工具提示开始与其他数据点重叠。 But with limited space you can't ignore the values.但是由于空间有限,您不能忽略这些值。 Hence it's better to zoom out or zoom in.因此,最好缩小或放大。

Using plotly使用情节

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

As a one liner using list comprehension and numpy:作为使用列表理解和 numpy 的单行程序:

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

setup is ditto to Rutger's answer.设置与罗格的回答同上。

This might be useful when you need individually annotate in different time (I mean, not in a single for loop)当您需要在不同时间单独注释时,这可能很有用(我的意思是,不是在单个 for 循环中)

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

where x and y are the your target coordinate and type is float/int.其中xy是您的目标坐标,类型是浮点数/整数。

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

相关问题 Matplotlib 散点图,每个点都有不同的文本 - Matplotlib scatter plot with different text at each point 在与标记的大小和颜色相匹配的每个数据点处使用不同文本的散点图 - Scatter plot with different text at each data point that matches the size and colour of the marker 在每个数据点具有对齐注释的散点图 - scatter plot with aligned annotations at each data point 如何在matplotlib中为散点图中的每个点绘制不同深浅的颜色? - How to plot different shades of a color for each point in a scatter plot in matplotlib? Python matplotlib:如何为散点图中的每个点分配不同的不透明度? - Python matplotlib: How to assign a different opacity to each point in a scatter plot? pandas - 散布 plot,每个点都有不同的颜色图例 - pandas - scatter plot with different color legend for each point 指定散点图中每个点的颜色(matplotlib) - Specify color of each point in scatter plot (matplotlib) Matplotlib:具有多个具有不同数据长度和文本的轴的散点图 - Matplotlib: scatter plot with multiple axes with different data lengths and text 使用熊猫突出显示散点图中的最后一个数据点 - Highlight last data point in Scatter plot with pandas Jupyter Python Notebook:交互式散点图,其中每个数据点与其自己的图形相关联? - Jupyter Python Notebook: interactive scatter plot in which each data point is associated with its own graph?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM