python绘图图表轴和变量

发布于 2025-01-11 15:04:05 字数 2882 浏览 0 评论 0原文

我正在使用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)

pic of the data:
enter image description here

dummy data :)

pic of the current result:

enter image description here

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文