简体   繁体   English

我可以让它在 plotly 子图中可编辑大小行高吗?

[英]Can I make it so the size row heights are editable in a plotly subplot?

I have this plot-我有这个情节-

from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig = make_subplots(rows=3, cols=1, row_heights = [0.3, 0.3, 0.4])

fig.append_trace(go.Scatter(
    x=[3, 4, 5],
    y=[1000, 1100, 1200],
), row=1, col=1)

fig.append_trace(go.Scatter(
    x=[2, 3, 4],
    y=[100, 110, 120],
), row=2, col=1)

fig.append_trace(go.Scatter(
    x=[0, 1, 2],
    y=[10, 11, 12]
), row=3, col=1)


fig.update_layout(height=600, width=600, title_text="Stacked Subplots")
fig.show()

I want it so that someone using the dashboard can adjust the size of each of the subplots.我想要它,以便使用仪表板的人可以调整每个子图的大小。 If they want to see the second subplot really big, for example, they could do that.例如,如果他们想看到第二个子情节非常大,他们可以这样做。 I have a version that works using custom buttons and passing along the values as an argument in the row_height field.我有一个使用自定义按钮并将值作为参数传递给 row_height 字段的版本。 I'm curious if there's a slider or some other way where the user could control the relative height of each of the subplots.我很好奇是否有 slider 或用户可以控制每个子图的相对高度的其他方式。

Generally you need callbacks for the user to specify arguments that modify the plot, but plotly-python does have buttons and dropdowns as you mentioned.通常,您需要用户callbacks来指定修改 plot 的 arguments,但 plotly-python 确实具有您提到的按钮和下拉菜单。

You can control subplots individually using a slider if you use plotly-dash and make each slider value an input to a function that updates the subplot row heights.如果您使用 plotly-dash 并使每个 slider 值成为更新子图行高的 function 的输入,则可以使用 slider 单独控制子图。

Since you mentioned that you wanted the sliders to specify relative height, I divided each slider value by the sum of all slider values.由于您提到您希望滑块指定相对高度,因此我将每个 slider 值除以所有 slider 值的总和。 This means that if each slider has the same value, the subplots will all look the same.这意味着如果每个 slider 具有相同的值,则子图看起来都相同。 This is functional as long as you don't allow all sliders values to be set to 0, but this could be potentially confusing from a user's perspective (alternatively, you could make the slider values in absolute units like pixels).只要您不允许将所有滑块值设置为 0,此功能就可以使用,但从用户的角度来看,这可能会造成混淆(或者,您可以将 slider 值设为像素等绝对单位)。

from plotly.subplots import make_subplots
import plotly.graph_objects as go
from dash import Dash, dcc, html, Input, Output

app = Dash(__name__)

app.layout = html.Div([
    html.H4('Live adjustable graph-size'),
    html.P("Change figure heights:"),
    dcc.Slider(id='slider-1', min=0.1, max=1, step=0.1, value=0.3),
    dcc.Slider(id='slider-2', min=0.1, max=1, step=0.1, value=0.3),
    dcc.Slider(id='slider-3', min=0.1, max=1, step=0.1, value=0.4),
    dcc.Graph(id="graph"),
])

@app.callback(
    Output("graph", "figure"), 
    Input('slider-1', 'value'),
    Input('slider-2', 'value'),
    Input('slider-3', 'value'))
def resize_figure(height_1, height_2, height_3):

    ## calculate relative heights
    total_height = height_1 + height_2 + height_3
    relative_heights = [height / total_height for height in [height_1, height_2, height_3]]
    fig = make_subplots(rows=3, cols=1, row_heights = relative_heights)

    fig.append_trace(go.Scatter(
        x=[3, 4, 5],
        y=[1000, 1100, 1200],
    ), row=1, col=1)

    fig.append_trace(go.Scatter(
        x=[2, 3, 4],
        y=[100, 110, 120],
    ), row=2, col=1)

    fig.append_trace(go.Scatter(
        x=[0, 1, 2],
        y=[10, 11, 12]
    ), row=3, col=1)


    fig.update_layout(height=600, width=600, title_text="Stacked Subplots")

    return fig

app.run_server(debug=True)

在此处输入图像描述

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

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