python绘图图表轴和变量
我正在使用plotly来显示来自xlsx的数据, 一切工作正常,除了我无法控制图中的轴, 列的头部包含月份的名称,这就是 X 轴中需要的内容, 第一行包含我需要将其放在图表变量中的事件类型,
这里是代码:
import dash
import os
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px
import pandas as pd
df = pd.read_excel( "2022.xlsx",
engine='openpyxl')
external_stylesheets = ["https://codepen.io/chriddyp/pen/bWLwgP.css"]
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
html.Div(
dcc.Graph(id='graph',
style={
'height': 500,
'width': 900,
"display": "block",
"margin-left": "auto",
"margin-right": "auto",}),
),
html.Div([
html.Label(['Choose a graph:'],style={'font-weight': 'bold'}),
dcc.RadioItems(
id='drop',
options=[
{'label': 'graph1', 'value': 'graph1'},
{'label': 'graph2', 'value': 'graph2'},
{'label': 'graph3', 'value': 'graph3'},
],
value='age',
style={"width": "60%"}
),
])
])
@app.callback(
Output('graph', 'figure'),
[Input(component_id='drop', component_property='value')]
)
def build_graph(value):
if value == 'graph1':
return px.scatter(
df,
x=["January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December"],
y=["vul-report" , "resk" , "reports" , "web" , "scan" , "email" , "call" , "open-src"],
size_max=50,
)
if value == 'graph2':
return px.bar(
df,
x=["January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December"],
y=["vul-report" , "resk" , "reports" , "web" , "scan" , "email" , "call" , "open-src"],
)
else:
return px.bar(
df,
x=["January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December"],
y=["vul-report" , "resk" , "reports" , "web" , "scan" , "email" , "call" , "open-src"],
barmode="group",
)
if __name__ == '__main__':
app.run_server(debug=True)
虚拟数据:)
当前结果的图片:
谢谢!
I'm using plotly to display data from xlsx,
everything is working fine except I can't control the axis in the graph,
the head of the columns contains the month's name and that is what needs to be in the X-axis,
the first row contains the type of events which I need it to be in the variable of the graph,
here is the code:
import dash
import os
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px
import pandas as pd
df = pd.read_excel( "2022.xlsx",
engine='openpyxl')
external_stylesheets = ["https://codepen.io/chriddyp/pen/bWLwgP.css"]
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
html.Div(
dcc.Graph(id='graph',
style={
'height': 500,
'width': 900,
"display": "block",
"margin-left": "auto",
"margin-right": "auto",}),
),
html.Div([
html.Label(['Choose a graph:'],style={'font-weight': 'bold'}),
dcc.RadioItems(
id='drop',
options=[
{'label': 'graph1', 'value': 'graph1'},
{'label': 'graph2', 'value': 'graph2'},
{'label': 'graph3', 'value': 'graph3'},
],
value='age',
style={"width": "60%"}
),
])
])
@app.callback(
Output('graph', 'figure'),
[Input(component_id='drop', component_property='value')]
)
def build_graph(value):
if value == 'graph1':
return px.scatter(
df,
x=["January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December"],
y=["vul-report" , "resk" , "reports" , "web" , "scan" , "email" , "call" , "open-src"],
size_max=50,
)
if value == 'graph2':
return px.bar(
df,
x=["January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December"],
y=["vul-report" , "resk" , "reports" , "web" , "scan" , "email" , "call" , "open-src"],
)
else:
return px.bar(
df,
x=["January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December"],
y=["vul-report" , "resk" , "reports" , "web" , "scan" , "email" , "call" , "open-src"],
barmode="group",
)
if __name__ == '__main__':
app.run_server(debug=True)
dummy data :)
pic of the current result:
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论