带有 2 个数据系列的条形图,带有 Pandas Dataframe 和 Plotly

Bar chart with 2 data series with Pandas Dataframe and Plotly

我有一个包含以下数据的数据框(示例):

year month count
2020 11 100
12 50
2021 01 80
02 765
03 100
04 265
05 500

我想用 plotly 将其绘制在条形图上,其中每个月有 2 个垂直条,一个用于 2020 年,另一个用于 2021 年。我想要轴根据数据集上可能发生变化的现有值自动定义。今天仅适用于 2020 年和 2021 年,但可能会有所不同。

我搜索过信息,但总是提到硬编码的数据集系列名称和数据,我不明白如何在 ploty 中动态输入这些。

我期待这样的结果,但它不起作用:

import plotly.express as px

...

px.bar(df, x=['year','month'], y='count')
fig.show()

谢谢,

要获得每个月的两个垂直条,我猜痕迹应该代表每一年。在这种情况下,您可以使用:

for y in df.year.unique():
    dfy = df[df.year == y]
    fig.add_bar(x = dfy.month, y = dfy.value, name = str(y))

情节 1

不过,这是您的有限数据集的结果。如果稍微扩展数据集,您会对它的外观有更好的印象:

情节 2

完整代码:

import plotly.graph_objects as go
import pandas as pd

df = pd.DataFrame({'year': {0: 2020, 1: 2020, 2: 2021, 3: 2021, 4: 2021, 5: 2021, 6: 2021},
                     'month': {0: 11, 1: 12, 2: 1, 3: 2, 4: 3, 5: 4, 6: 5},
                     'value': {0: 100, 1: 50, 2: 80, 3: 765, 4: 100, 5: 265, 6: 500}})

df = pd.DataFrame({'year': {0: 2020,
                  1: 2020,
                  2: 2020,
                  3: 2020,
                  4: 2020,
                  5: 2020,
                  6: 2020,
                  7: 2020,
                  8: 2020,
                  9: 2020,
                  10: 2020,
                  11: 2020,
                  12: 2021,
                  13: 2021,
                  14: 2021,
                  15: 2021,
                  16: 2021,
                  17: 2021,
                  18: 2021,
                  19: 2021,
                  20: 2021,
                  21: 2021,
                  22: 2021,
                  23: 2021},
                 'month': {0: 1,
                  1: 2,
                  2: 3,
                  3: 4,
                  4: 5,
                  5: 6,
                  6: 7,
                  7: 8,
                  8: 9,
                  9: 10,
                  10: 11,
                  11: 12,
                  12: 1,
                  13: 2,
                  14: 3,
                  15: 4,
                  16: 5,
                  17: 6,
                  18: 7,
                  19: 8,
                  20: 9,
                  21: 10,
                  22: 11,
                  23: 12},
                 'value': {0: 100,
                  1: 50,
                  2: 265,
                  3: 500,
                  4: 80,
                  5: 765,
                  6: 100,
                  7: 265,
                  8: 500,
                  9: 80,
                  10: 765,
                  11: 100,
                  12: 80,
                  13: 765,
                  14: 100,
                  15: 265,
                  16: 500,
                  17: 80,
                  18: 765,
                  19: 100,
                  20: 265,
                  21: 500,
                  22: 80,
                  23: 765}})

fig = go.Figure()
for y in df.year.unique():
    dfy = df[df.year == y]
    fig.add_bar(x = dfy.month, y = dfy.value, name = str(y))

fig.show()
import pandas as pd
import io
import plotly.express as px
import plotly.graph_objects as go

df = pd.read_csv(
    io.StringIO(
        """year,month,count
2020,1,50
2020,2,50
2020,3,50
2020,4,50
2020,11,100
2020,12,50
2021,1,80
2021,2,765
2021,3,100
2021,4,265
2021,5,500"""
    )
)

go.Figure(go.Bar(x=[df["month"].tolist(), df["year"].tolist()], y=df["count"]))

使用 Plotly Express 并使用多分类 x 轴更新:

import pandas as pd
import io
import plotly.express as px

df = pd.read_csv(
    io.StringIO(
        """year,month,count
2020,1,50
2020,2,50
2020,3,50
2020,4,50
2020,11,100
2020,12,50
2021,1,80
2021,2,765
2021,3,100
2021,4,265
2021,5,500"""
    )
)

# convert year to string so you get a catergorical scale
df['year'] = df['year'].astype(str)

channel_top_Level = "year"
channel_2nd_Level = "month"

fig = px.bar(df, x = channel_2nd_Level, y = 'count', color = channel_top_Level)
for num,channel_top_Level_val in enumerate(df[channel_top_Level].unique()):
    temp_df = df.query(f"{channel_top_Level} == {channel_top_Level_val !r}")
    fig.data[num].x = [
            temp_df[channel_2nd_Level].tolist(), 
            temp_df[channel_top_Level].tolist() 
          ]
fig.layout.xaxis.title.text = f"{channel_top_Level} / { channel_2nd_Level}"
fig