Plotly:xaxis不会填充整个系列

2024-09-26 18:09:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图绘制一个条形图,销售vs类别。下面是我的数据框

  S2PName-Category  S2BillDate   totSale  count
0        Beverages  2020-03-06   4452.62    252
1             Food  2020-03-06  36556.91    774
2           Others  2020-03-06    608.95     99
3           Snacks  2020-03-06   2662.75    139
4            Juice  2020-03-06   2139.49     40
5         IceCream  2020-03-06    135.00      4
6              OOB  2020-03-06    390.00     20

我的代码:

data = [go.Bar(x=df['S2PName-Category'].unique(),
        y=df[df['S2PName-Category']==category]['totSale'],
        name = category) for category in df['S2PName-Category'].unique()]

layout = go.Layout(title='Category wise performance',
        xaxis = dict(title = 'Categories', automargin = True),
        yaxis = dict(tickprefix= '₹', tickformat=',.2f',type='log',autorange = True),
        hovermode = 'closest',
        plot_bgcolor =  colors['background'],
        paper_bgcolor = colors['background'],
        font = dict(color = colors['text'])
        )

我的图表: enter image description here

只有类别(“S2PName-category”)中的饮料在x轴上有分布,其余饮料没有分布。有人能指出哪里出了问题,以及如何解决这个问题吗?谢谢

试验2:我在尝试其他可能性,由于列表理解,我的条形图被分组,有没有办法得到要绘制的唯一类别

代码:

data = [go.Bar(x=df['S2PName-Category'],
        y=df['totSale'],
        name = category) for category in df['S2PName-Category'].unique()]

    layout = go.Layout(title='Category wise performance',
        xaxis = dict(title = 'Categories', automargin = True),
        yaxis = dict(tickprefix= '₹', tickformat=',.2f',type='log',autorange = True),
        hovermode = 'closest',
        plot_bgcolor =  colors['background'],
        paper_bgcolor = colors['background'],
        font = dict(color = colors['text'])
        )

图Op: enter image description here


Tags: truegodftitle绘制类别dictunique
1条回答
网友
1楼 · 发布于 2024-09-26 18:09:12

如果您提供的数据样本实际上代表了您的真实数据源,那么您可以设置一个带有go.Figure()的图形,并为每个'S2PName-Category'添加一个单独的跟踪 如果这还不够,您必须提供更好的数据示例

enter image description here

完整代码:

import plotly.express as px
import pandas as pd
import plotly.graph_objs as go

df = pd.DataFrame({'S2PName-Category': {0: 'Beverages',
                    1: 'Food',
                    2: 'Others',
                    3: 'Snacks',
                    4: 'Juice',
                    5: 'IceCream',
                    6: 'OOB'},
                    'S2BillDate': {0: '2020-03-06',
                    1: '2020-03-06',
                    2: '2020-03-06',
                    3: '2020-03-06',
                    4: '2020-03-06',
                    5: '2020-03-06',
                    6: '2020-03-06'},
                    'totSale': {0: 4452.62,
                    1: 36556.91,
                    2: 608.95,
                    3: 2662.75,
                    4: 2139.49,
                    5: 135.0,
                    6: 390.0},
                    'count': {0: 252, 1: 774, 2: 99, 3: 139, 4: 40, 5: 4, 6: 20}})


fig = go.Figure()
for cat in df['S2PName-Category']:
    dft = df[df['S2PName-Category']==cat]
    fig.add_traces(go.Bar(x=dft['S2PName-Category'], y=dft['totSale'], name=cat))

fig.show()

相关问题 更多 >

    热门问题