gpt4 book ai didi

python - 使用 Dash (Python) 移动对象(条形图)

转载 作者:太空宇宙 更新时间:2023-11-04 02:22:40 33 4
gpt4 key购买 nike

我想把一辆小车移到页面中间。我使用以下代码创建了条形图:

# -*- coding: utf-8 -*-
import dash
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash()

app.layout = html.Div(children=[
html.H1(children='Hello Dash'),

html.Div(children='''
Dash: A web application framework for Python.
'''),

dcc.Graph(
id='example-graph',
style={
'height': 500,
'width': 900,
'display': 'inline-block'},
figure={
'data': [
{'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
{'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
],
'layout': {
'title': 'Dash Data Visualization'
}

}

)
])

if __name__ == '__main__':
app.run_server(debug=True)

我想把这个图表移到页面中间。有没有一种方法可以指定关于条形图应该去哪里的特定坐标?

最佳答案

如果你只想让图表居中,你必须将 margin-leftmargin-right 设置为 auto 并且你需要指定 display 到图形样式字典中的 block

像这样:

import dash
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash()

app.layout = html.Div(children=[
html.H1(children='Hello Dash'),

html.Div(children='''
Dash: A web application framework for Python.
'''),

dcc.Graph(
id='example-graph',
style={
'height': 500,
'width': 900,
"display": "block",
"margin-left": "auto",
"margin-right": "auto",
},
figure={
'data': [
{'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
{'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
],
'layout': {
'title': 'Dash Data Visualization'
}

}

)
])

if __name__ == '__main__':
app.run_server(debug=True)

导致此布局:

enter image description here

如果你希望一切都居中,你可以将 margin 指定为 auto 并将 width 设置为 50%像这样的父 div 元素:

# -*- coding: utf-8 -*-
import dash
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash()

app.layout = html.Div(children=[
html.H1(children='Hello Dash'),

html.Div(children='''
Dash: A web application framework for Python.
'''),

dcc.Graph(
id='example-graph',
style={
'height': 500,
'width': 900,
},
figure={
'data': [
{'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
{'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
],
'layout': {
'title': 'Dash Data Visualization'
}

}

)
],style = {'margin':'auto','width': "50%"})

if __name__ == '__main__':
app.run_server(debug=True)

导致此布局:

enter image description here

关于python - 使用 Dash (Python) 移动对象(条形图),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51193845/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com