18

我在 Dash 上的图上遇到了布局困难。我使用 Dash 生成的所有绘图似乎都自动调整为非常窄的尺寸,这使得如果没有一些创造性的缩放就很难实际查看数据。

例如,当我在一个破折号上查看源代码时,它看起来好像计算出主绘图容器(svg-container)的高度为 450 像素,图形本身的高度为 270 像素(查看子图)。如果我可以制作图表,比如 700 像素,那就太好了。

我的问题是:在 Dash 上调整图形尺寸的最佳方法是什么?我的第一个猜测是以某种方式附加样式表,但我不确定适当的 css 选择器将如何或是什么。

谢谢!

4

3 回答 3

32

或者,您可以更改父容器中的视口大小,例如:

dcc.Graph(id='my-graph',style={'width': '90vh', 'height': '90vh'}) 

这会将图形更改为浏览器视口高度的 90%。您可以在此链接上查看更多视口信息。

于 2020-11-25T11:43:10.033 回答
22

一个Graph对象包含一个figure. 每个figure都有datalayout属性。

您可以heightlayout.

dcc.Graph(
    id="my-graph",
    figure={
        "data": [
            {"x": [1, 2, 3], "y": [4, 1, 2], "type": "bar"},
            {"x": [1, 2, 3], "y": [2, 4, 5], "type": "bar"},
        ],
        "layout": {
            "title": "My Dash Graph",
            "height": 700,  # px
        },
    },
)

根据Plotly figureobject schemaheight必须是大于等于 10 的数字,默认为 450 (px)。

请记住,您可以在破折号回调中创建一个Graph对象并稍后设置。figure

例如,如果valueadcc.Slider影响您的figure属性,您Graph将拥有:

import plotly.graph_objs as go

dcc.Graph(id="my-graph")

@app.callback(
    output=Output("my-graph", "figure"),
    inputs=Input("slider", "value")])
def update_my_graph(value):
    data = go.Data(
        [
            go.Bar(x=[1, 2, 3], y=[4, 1, 2]),
            go.Bar(x=[1, 2, 3], y=[2, 4, 5]),
        ]
    layout = go.Layout(
        title="My Dash Graph",
        height=700
        )
    figure = go.Figure(data=data, layout=layout)
    return figure
于 2018-04-13T17:59:13.297 回答
11

我通过将绘图的 div 作为子 div 放置在父 div 中来做到这一点,然后设置父 div 的大小。像这样的东西:

# main parent div for the app
main_div = html.Div(children = [
    # sub-div for the plot
    html.Div(children = [
                dcc.Graph(id = 'my-plot'),
    ])
    ],
    # set the sizing of the parent div
    style = {'display': 'inline-block', 'width': '48%'})

随着您的应用程序变得越来越复杂,您可能需要设置更多的 div 嵌套才能使其正常工作。而且您也可以style直接在绘图的子 div 上设置 ,具体取决于您的配置方式。

另外,我可能会建议关注官方 Dash 论坛,因为那里可能会有更多用户,以及 Dash 开发者本人经常发帖:https ://community.plot.ly/c/dash

于 2017-09-18T20:03:34.410 回答