Plotly不会从枢轴绘制条形图

问题描述 投票:0回答:1

[我正在尝试从我使用透视表转换的CSV数据中绘制条形图。条形图应在y轴上具有计数,在x轴上具有公司状态。

我得到的是:Bar chart not showing

最终,我想按CompanySizeId堆叠栏。

我一直关注this video

import plotly.graph_objects as go
import plotly.offline as pyo
import pandas as pd

countcompany = pd.read_csv(
    'https://raw.githubusercontent.com/redbeardcr/Plotly/master/Data/countcompany.csv')

df = pd.pivot_table(countcompany, index='CompanyStatusLabel',
                    values='n', aggfunc=sum)
print(df)

data = [go.Bar(
    x=df.index,
    y=df.values,
)]

layout = go.Layout(title='Title')

fig = go.Figure(data=data, layout=layout)

pyo.plot(fig)

可以找到代码here

感谢您的帮助

python pandas plotly
1个回答
0
投票

[如果使用y值对数组进行展平,即如果将y=df.values替换为y=df.values.flatten(),则代码将按预期工作。

import plotly.graph_objects as go
import plotly.offline as pyo
import pandas as pd

countcompany = pd.read_csv(
    'https://raw.githubusercontent.com/redbeardcr/Plotly/master/Data/countcompany.csv')

df = pd.pivot_table(countcompany, index='CompanyStatusLabel',
                    values='n', aggfunc=sum)
print(df)

data = [go.Bar(
    x=df.index,
    y=df.values.flatten(),
)]

layout = go.Layout(title='Title')

fig = go.Figure(data=data, layout=layout)

pyo.plot(fig)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.