[英]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))
不過,這就是您有限數據集的結果。 如果您稍微擴展數據集,您將對它的外觀有更好的印象:
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.