python - Python Plotly:如何将图像添加到 3D 散点图中
问题描述
我试图在 z 轴为时间的 3D 散点图中可视化多个 2d 轨迹 (x, y)。
import numpy as np
import pandas as pd
import plotly.express as px
# Sample data: 3 trajectories
t = np.linspace(0, 10, 200)
df = pd.concat([pd.DataFrame({'x': 900 * (1 + np.cos(t + 5 * i)), 'y': 400 * (1 + np.sin(t)), 't': t, 'id': f'id000{i}'}) for i in [0, 1, 2]])
# 3d scatter plot
fig = px.scatter_3d(df, x='x', y='y', z='t', color='id', )
fig.update_traces(marker=dict(size=2))
fig.show()
我有一个大小为 2000x1000 的地图的 .png 文件。轨迹的 (x, y) 坐标对应于地图的像素位置。
我想在 3d 散点图的“地板”上看到地图的图像。
我尝试使用以下代码添加图像:
from scipy import misc
img = misc.imread('images/map_bg.png')
fig2 = px.imshow(img)
fig.add_trace(fig2.data[0])
fig.show()
如果我旋转/缩放,我想要散点图“地板”上的图像并随着散点图移动。这是一个模拟:
附加说明:可以有任意数量的轨迹,对于我的应用程序,重要的是每个轨迹都自动绘制成不同的颜色。我正在使用plotly.express
,但只要满足这些要求,我就可以使用其他 plotly 包。
解决方案
我遇到了同样的情况,我想在 3D 散点图中使用图像作为底面。在此处和此处的两个帖子的帮助下,我已经能够创建以下 3d 散点图:
我在示例中使用了 plotly go,因此结果与 OP 中的代码略有不同。
import numpy as np
import pandas as pd
from PIL import Image
import plotly.graph_objects as go
from scipy import misc
im = misc.face()
im_x, im_y, im_layers = im.shape
eight_bit_img = Image.fromarray(im).convert('P', palette='WEB', dither=None)
dum_img = Image.fromarray(np.ones((3,3,3), dtype='uint8')).convert('P', palette='WEB')
idx_to_color = np.array(dum_img.getpalette()).reshape((-1, 3))
colorscale=[[i/255.0, "rgb({}, {}, {})".format(*rgb)] for i, rgb in enumerate(idx_to_color)]
# Sample data: 3 trajectories
t = np.linspace(0, 10, 200)
df = pd.concat([pd.DataFrame({'x': 400 * (1 + np.cos(t + 5 * i)), 'y': 400 * (1 + np.sin(t)), 't': t, 'id': f'id000{i}'}) for i in [0, 1, 2]])
# im = im.swapaxes(0, 1)[:, ::-1]
colors=df['t'].to_list()
# # 3d scatter plot
x = np.linspace(0,im_x, im_x)
y = np.linspace(0, im_y, im_y)
z = np.zeros(im.shape[:2])
fig = go.Figure()
fig.add_trace(go.Scatter3d(
x=df['x'],
y=df['y'],
z=df['t'],
marker=dict(
color=colors,
size=4,
)
))
fig.add_trace(go.Surface(x=x, y=y, z=z,
surfacecolor=eight_bit_img,
cmin=0,
cmax=255,
colorscale=colorscale,
showscale=False,
lighting_diffuse=1,
lighting_ambient=1,
lighting_fresnel=1,
lighting_roughness=1,
lighting_specular=0.5,
))
fig.update_layout(
title="My 3D scatter plot",
width=800,
height=800,
scene=dict(xaxis_visible=True,
yaxis_visible=True,
zaxis_visible=True,
xaxis_title="X",
yaxis_title="Y",
zaxis_title="Z" ,
))
fig.show()
推荐阅读
- python - 在 python 中使用 Stanford Corenlp for Chinese
- python - python pyautogui 与 pynput 一起工作
- php - 如何从图像 url 获取图像名称和图像扩展名
- ios - 如何在登录用户通过 Facebook 在 firebase for ios swift 中登录后检查?
- python - 如何在 SVM 中操作多维特征或使用多维特征训练模型?
- ios - 如果在类扩展中声明的任何方法都是静态的,我会感到困惑
- ios - 如何解密 AES 256 https://aesencryption.net/ - IOS
- javascript - 文本框中带有 JavaScript 的文件 (ZK)
- reactjs - 使用 Jest 进行反应测试
- java - JavaFx:TableViewSkin 初始化