繁体   English   中英

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

[英]Bar chart with 2 data series with Pandas Dataframe and Plotly

我有一个 dataframe 具有以下数据(示例):

数数
2020 11 100
12 50
2021 01 80
02 765
03 100
04 265
05 500

我想将 plot 与plotly放在条形图上,每个月有 2 个垂直条,一个用于 2020 年,另一个用于 2021 年。我希望根据数据集上的现有值自动定义轴,这可以改变。 今天仅适用于 2020 年和 2021 年,但可能会有所不同。

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

我期待这样的事情,但它不起作用:

import plotly.express as px

...

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

谢谢,

尝试:

df.set_index((['year', 'month']))['count'].unstack(0).plot()

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

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

Plot 1

在此处输入图像描述

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

Plot 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

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM