情节:如何用字符串列表和坐标列表注释图形? [英] Plotly: How to annotate figure by a list of strings and a list of coordinates?

查看:105
本文介绍了情节:如何用字符串列表和坐标列表注释图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用matplotlib绘制长度为D的特定列表Z [i,j]以及从另一个列表index_word_map [i]中获取的点的注释,如下所示:

I am plotting a certain list, Z[i, j] of length D, using matplotlib along with the annotation of the points taken from another list, index_word_map[i], as follows:

plt.scatter(Z[:,0], Z[:,1])
for i in range(D):
    plt.annotate(s=index_word_map[i], xy=(Z[i,0], Z[i,1]))
plt.show()

现在,我想使用plotly或plotly express进行相同的操作.我可以使用plotly express来绘制点:

Now, I want to do the same using plotly or plotly express. I can plot the points using plotly express as follows:

X = []
Y = []
for i in range(D):
    x = Z[i,0]
    y = Z[i,1]
    X.append(x)
    Y.append(y)
fig = px.scatter(x=X, y=Y)

但是,我该如何使用注释列表(index_word_map [i])使点的标签也显示在绘图上?

But, how am I to use the annotation list (index_word_map[i]) to have the labels of the points to show up on the plot also?

推荐答案

如果我理解正确,那么您希望实现以下目标:

If I understand correctly, you'd like to achieve something like this:

您在这里看到的是一些以散列颜色为主题的列表D,这些主题用作对xy值的散点图的注释,这些散点图被组织为列表Z的列表.

What you're looking at here is a list D of some plotly color themes used as annotations for a scatter plot of x and y values organized as a list of lists Z.

D='Alphabet','Antique','Bold','D3','Dark2','Dark24','G10','Light24','Pastel','Pastel1']

Z=[[0, 0],[1, 2],[2, 4],[3, 6],[4, 8],[5, 10],[6, 12],[7, 14],[8, 16],[9, 18]]

完整代码:

import numpy as np
import plotly.express as px
import plotly.graph_objs as go

# sample data
D='Alphabet','Antique','Bold','D3','Dark2','Dark24','G10','Light24','Pastel','Pastel1']

# sample of two lists
z1 =np.arange(len(D)).tolist()
z2 = [i*2 for i in z1]

# turn two lists into a list of tuples
# containing each element of z1 of z2
# as zipped pairs
tuplist = list(zip(z1, z2))

# turn the list of tuples into list of lists
Z=[list(elem) for elem in tuplist]


# plotly setup
fig = go.Figure(data=go.Scatter(x=z1, y=z2, marker_color='black'))

for i, m in enumerate(D):
    fig.add_annotation(dict(font=dict(color='rgba(0,0,200,0.8)',size=12),
                                        x=Z[i][0],
                                        y=Z[i][1],
                                        showarrow=False,
                                        text=D[i],
                                        textangle=0,
                                        xanchor='left',
                                        xref="x",
                                        yref="y"))
fig.show()

我希望这是您想要的.不要犹豫,让我知道!

I hope this is what you were looking for. Don't hesitate to let me know if not!

这篇关于情节:如何用字符串列表和坐标列表注释图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆