繁体   English   中英

Python Plotly:如何将图像添加到 3D 散点图

[英]Python Plotly: How to add an image to a 3D scatter plot

我试图在 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()

原始 3D 散点图

我有一个大小为 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()


暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM