python - 将图像对象添加为 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()
解决方案
推荐阅读
- scala - 函数组合导致方法首先缺少参数列表
- html - 正确垂直对齐 Flexbox 项目
- math - 无论数字有多大,乘法/加法的速度是否都是常数?
- firebase - iOS 14 小部件 + SwiftUI + Firebase?
- python - 为什么 Numpy 数组重塑需要两个括号?
- django - 用于成员模型的 Django 休息框架序列化程序?
- oracle - Oracle ORDS 公开带参数的打包过程
- pyspark - 迭代数据框和递归过滤器
- python-3.x - 如何在 matplot 图形窗口中添加“复制 id”按钮
- swift - 有没有办法在 Swift 中查看 UI 按钮的对象 ID?以编程方式创建的按钮