简体   繁体   English

Plotly:如何以交互方式更改 3D 散点图 plot 的轴变量?

[英]Plotly: How to change axes variables interactively for a 3D scatter plot?

I'm plotting a multidimensional table in scatter plots to test relationships between columns of a unique data frame.我在散点图中绘制了一个多维表,以测试唯一数据框的列之间的关系。

I would like to know if there is any way that I can change in the browser the variable in each ax in an interactive way, without needing to plot another grid.我想知道是否有任何方法可以在浏览器中以交互方式更改每个轴中的变量,而无需 plot 另一个网格。

I don't know if this is a redundant question, but I've already did some research without any success.我不知道这是否是一个多余的问题,但我已经做了一些研究但没有任何成功。

fig = px.scatter_3d(data, x="V1", y="V2", z= 'V3', hover_data=['Z'])
fig.show()

Thank you in advance.先感谢您。

The complete code snippet below will give you a Dash-app in JupyterLab that looks like this:下面的完整代码片段将为您提供 JupyterLab 中的 Dash 应用程序,如下所示:

在此处输入图像描述

Here you can change which columns to display from the plotly dataset px.data.stocks() .在这里,您可以从 plotly 数据集px.data.stocks()更改要显示的列。 If this approach is something you can use, I'd be happy to explain the details.如果您可以使用这种方法,我很乐意解释细节。 ANd if JupyterLab is not your thing, just follow the three steps needed to rewrite it to a standard Dash app as described in the post Plotly: How to rewrite a standard dash app to launch it in JupyterLab?如果 JupyterLab 不是您的菜,只需按照Plotly 帖子中所述将其重写为标准 Dash 应用程序所需的三个步骤:如何重写标准 dash 应用程序以在 JupyterLab 中启动它?

Complete code_完整代码_

import plotly as py
import pandas as pd

from plotly import tools
import plotly.express as px
import plotly.graph_objects as go

import dash
import dash_core_components as dcc
import dash_html_components as html
from jupyter_dash import JupyterDash
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output


app = JupyterDash(external_stylesheets=[dbc.themes.SLATE])


colors = px.colors.qualitative.Plotly
# colors = ['blue', 'green', 'red', 'black', 'yellow']
symbols = ['circle', 'circle-open', 'square', 'square-open', 'diamond', 'diamond-open', 'cross', 'x']

df = px.data.stocks().set_index('date')
columns = df.columns

# Set up well organized controls in a dbc.Card()
controls = dbc.Card([dbc.FormGroup([dbc.Label("x-axis"),
                                    dcc.Dropdown(id='dd_x',
                                                 options= [{'label': k, 'value': k} for k in columns],
                                                  value=columns[0],
                                                ),
                                   ],),
                    dbc.FormGroup([dbc.Label("y-axis"),
                                   dcc.Dropdown(id='dd_y',
                                                options= [{'label': k, 'value': k} for k in columns],
                                                value=columns[1],
                                                ),
                                   ],),
                    dbc.FormGroup([dbc.Label("z-axis"),
                                   dcc.Dropdown(id='dd_z',
                                                options= [{'label': k, 'value': k} for k in columns],
                                                value=columns[2],
                                                ),
                                    ],)
                    ],
                    body=True,
                    style = {'font-size': 'large'}
                    )

# Set up the app layout using dbc.Container(), dbc.Row(), and dbc.Col()
app.layout = dbc.Container([html.H1("Make a column selection for each axis"),
                            html.Hr(),
                            dbc.Row([dbc.Col([controls],xs = 4),
                                     dbc.Col([dbc.Row([dbc.Col(dcc.Graph(id="market_graph")),])]),
                                    ]),
                            html.Br(),
                            ],
                            fluid=True,
                            )

# 3D figure with callbacks for color, symbol and size
@app.callback(
    Output("market_graph", "figure"),
    [
        Input("dd_x", "value"),
        Input("dd_y", "value"),
        Input("dd_z", "value"),
    ],
)
def history_graph(x, y, z):
#     df = px.data.iris()
    fig = px.scatter_3d(df, x=df[x], y=df[y], z=df[z])

    fig.data[0].update(marker_color=colors[4])
    fig.data[0].update(marker_symbol=symbols[6])
    fig.data[0].update(marker_size=8)

    fig.update_layout(uirevision='constant')
    fig.update_layout(template = 'plotly_dark')
    fig.update_layout(margin=dict(l=10, r=10, b=10, t=10))
    return fig

app.run_server(mode='inline', port = 8007)

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

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