[英]Plotly: x-axis does not populate the entire series
I am trying to plot a bar graph, Sale vs categories.我正在尝试 plot 条形图,销售与类别。 Below is my data frame,
下面是我的数据框,
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
My Code:我的代码:
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'])
)
Only Beverages from the category ('S2PName-Category'), ispopulated in x-axis, rest are not getting populated.只有类别('S2PName-Category')中的饮料填充在 x 轴上,rest 未填充。 Can anybody point out what went wrong, and how to solve this?
任何人都可以指出出了什么问题,以及如何解决这个问题? Thanks !
谢谢 !
TRIAL 2: I was trying out other possibilties, due to list comprehension my bar chart gets grouped, is there a way to get unique categories to be plotted?试验 2:我正在尝试其他可能性,由于列表理解,我的条形图被分组,有没有办法绘制独特的类别?
Code:代码:
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'])
)
If your provided data sample does in fact represent your real data source, you can just set up a figure with go.Figure()
and add an individual trace for each 'S2PName-Category'
If this does not suffice, you will have to provide a better data sample.如果您提供的数据样本确实代表了您的真实数据源,您可以使用
go.Figure()
设置一个图形,并为每个'S2PName-Category'
添加一个单独的跟踪如果这还不够,您将不得不提供更好的数据样本。
Complete code:完整代码:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.