首页 > 解决方案 > 将图像对象添加为 Plotly 图例条目

问题描述

我是新手Plotly,但我还没有弄清楚如何在图中添加图像作为图例条目。下面是我的图的一个最小工作示例,其中我的六个站点图现在绘制在图本身而不是图例条目中。更具体地说,我希望1-1-1-1-0-0用图形结构替换图例条目(参见图示例)。

先感谢您!

import numpy as np
import plotly

raw_symbols = [0, 1, 2, 5, 6, 7, 8, 9, 10, 11, 12, 100, 101, 102, 105, 106, 107, 108, 109, 110, 111, 112]
color_dict = {"0": "gray", "1": "white"}

xyz = np.array([[ 0.      ,  1.385535],
       [ 1.199909,  0.692768],
       [ 1.199909, -0.692768],
       [ 0.      , -1.385535],
       [-1.199909, -0.692768],
       [-1.199909,  0.692768]])
xyz[:, 1] *= 3.2

A_matrix = np.array([[0, 1, 0, 0, 0, 1],
                    [1, 0, 1, 0, 0, 0],
                    [0, 1, 0, 1, 0, 0],
                    [0, 0, 1, 0, 1, 0],
                    [0, 0, 0, 1, 0, 1],
                    [1, 0, 0, 0, 1, 0]])

data = []


for i, graph in enumerate(["1-1-1-1-0-0", "1-1-1-0-1-0", "1-1-0-1-1-0"]):
    data.append(plotly.graph_objects.Scatter(x=np.linspace(0, 20, 50), y=np.linspace(0, 20, 50)*i, 
                             mode="markers",
                             name=graph,
                             marker_symbol=raw_symbols[i],
                             marker_line_color="black", marker_color="black")
    )
    
    colors = []

    for s in graph:
        if s != "-":
            colors.append(color_dict[s])

    for atom_idx, atom in enumerate(A_matrix):
        adjacencies = np.where(atom == 1)[0]

        for adj in adjacencies:
            data.append(plotly.graph_objects.Scatter(x=[xyz[atom_idx][0], xyz[adj][0]], y=[xyz[atom_idx][1], xyz[adj][1]],
                                                    line_color="black", showlegend=False)
            )
    data.append(plotly.graph_objects.Scatter(x=xyz[:, 0], y=xyz[:, 1],
                                              mode="markers",
                                              marker_symbol=1,
                                              marker_size=10,
                                              marker_line_color="black",
                                              marker_line_width=1.5,
                                              marker_color=colors,
                                              showlegend=False
                                              )
    )


layout = plotly.graph_objects.Layout(
                legend=dict(
                    orientation="h"
                ),
                title=dict(
                    text="test",
                    x=0.5,
                    y=0.9,
                    xanchor="center",
                    yanchor="top"
                ),
                plot_bgcolor='rgb(255, 255, 255)',
                xaxis_title=r"$x$",
                yaxis_title=r"$y$",
                font_family="sans serif",
                width=1000, height=650,
                xaxis=dict(
                    showline=True,
                    linewidth=1,
                    ticks="inside",
                    linecolor="black",
                    mirror=True
                ),
                yaxis=dict(
                    showline=True,
                    linewidth=1,
                    ticks="inside",
                    linecolor="black",
                    mirror=True
                )
)
    
fig = plotly.graph_objects.Figure(data=data, layout=layout)
fig.show()

在此处输入图像描述

标签: pythonimageplotly

解决方案


推荐阅读