如何在 Plotly Dash 中设置数据标签
How to set data labels in Plotly Dash
我是 Dash
的新手(今天刚开始),我正在尝试向玩具数据集添加一些数据标签。
我的代码如下:
import dash
import dash_html_components as html
import dash_core_components as dcc
#start the application
app = dash.Dash()
app.layout = html.Div(children=[html.H1(children='Modes of Transportation'),
html.Div(children = 'Explanatory text would be inserted here...'),
dcc.Graph(id='dash_graph',
figure = {'data': [{'x':['red', 'white', 'blue'], 'y':[10, 20, 30], 'type': 'bar', 'name':'Cars'},
{'x':['red', 'white', 'blue'], 'y':[5, 10, 15], 'type': 'bar', 'name':'Boats'},
{'x':['red', 'white', 'blue'], 'y':[2, 4, 6], 'type': 'bar', 'name':'Trains'},
],
'layout':{'title':'Modes of Transportation',
'xaxis':{'title':'Mode', 'type':'category'},
'yaxis':{'title':'Trips per Hour'}
}
}),
dcc.Graph(id='dash_graph two',
figure = {'data': [{'x':['orange', 'purple', 'blue'], 'y':[1, 2, 3], 'type': 'bar', 'name':'Mode'},],
'layout':{'title':'Modes of Transportation', 'xaxis':{'title':'Mode', 'type':'category'}, 'yaxis':{'title':'Per Hour'}}})
])
if __name__ == '__main__':
app.run_server(debug = True)
通过阅读文档,我发现我需要设置 textposition
。我只是不知道在哪里设置它。任何建议将不胜感激。谢谢!
此文档页面 https://plotly.com/python/text-and-annotations/ 应该有所帮助!
您需要添加一个 text
数组作为 x
和 y
的对等数组,然后在其旁边设置 textposition
。
我是 Dash
的新手(今天刚开始),我正在尝试向玩具数据集添加一些数据标签。
我的代码如下:
import dash
import dash_html_components as html
import dash_core_components as dcc
#start the application
app = dash.Dash()
app.layout = html.Div(children=[html.H1(children='Modes of Transportation'),
html.Div(children = 'Explanatory text would be inserted here...'),
dcc.Graph(id='dash_graph',
figure = {'data': [{'x':['red', 'white', 'blue'], 'y':[10, 20, 30], 'type': 'bar', 'name':'Cars'},
{'x':['red', 'white', 'blue'], 'y':[5, 10, 15], 'type': 'bar', 'name':'Boats'},
{'x':['red', 'white', 'blue'], 'y':[2, 4, 6], 'type': 'bar', 'name':'Trains'},
],
'layout':{'title':'Modes of Transportation',
'xaxis':{'title':'Mode', 'type':'category'},
'yaxis':{'title':'Trips per Hour'}
}
}),
dcc.Graph(id='dash_graph two',
figure = {'data': [{'x':['orange', 'purple', 'blue'], 'y':[1, 2, 3], 'type': 'bar', 'name':'Mode'},],
'layout':{'title':'Modes of Transportation', 'xaxis':{'title':'Mode', 'type':'category'}, 'yaxis':{'title':'Per Hour'}}})
])
if __name__ == '__main__':
app.run_server(debug = True)
通过阅读文档,我发现我需要设置 textposition
。我只是不知道在哪里设置它。任何建议将不胜感激。谢谢!
此文档页面 https://plotly.com/python/text-and-annotations/ 应该有所帮助!
您需要添加一个 text
数组作为 x
和 y
的对等数组,然后在其旁边设置 textposition
。