Rom*_*man 1 python plot bar-chart plotly
我有这样的数据
import pandas as pd
df = pd.DataFrame(
dict(
week=[1, 1, 2, 2, 3, 3] * 2,
layout=["classic", "classic", "modern", "modern"] * 3,
response=["conversion", "exit"] * 6,
cnt=[26, 23, 45, 34, 55, 44, 53, 27, 28, 25, 30, 34],))
Run Code Online (Sandbox Code Playgroud)
我不能使用两个类别的主要问题。我的代码:
px.bar(
data_frame=df,
x='week',
y='cnt',
template='plotly_dark',
color = 'layout'
)
Run Code Online (Sandbox Code Playgroud)
但我无法像 excel 示例中那样显示有关“响应”的信息
在我看来,最灵活的方法是使用go.Figure()然后
fig.add_traces(go.Bar(x=dfp['week'], y = dfp['cnt'], name = v))
Run Code Online (Sandbox Code Playgroud)
对于每个值, v,['conversion - classic', 'conversion - modern', 'exit - classic', 'exit - modern']像这样:
fig = go.Figure()
for v in df['value'].unique():
dfp = df[df['value']==v]
fig.add_traces(go.Bar(x=dfp['week'], y = dfp['cnt'], name = v))
fig.update_layout(barmode='stack', template='plotly_dark')
fig.show()
Run Code Online (Sandbox Code Playgroud)
据我所知,这应该非常类似于您的 Excel 输出。
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
df = pd.DataFrame(
dict(
week=[1, 1, 2, 2, 3, 3] * 2,
layout=["classic", "classic", "modern", "modern"] * 3,
response=["conversion", "exit"] * 6,
cnt=[26, 23, 45, 34, 55, 44, 53, 27, 28, 25, 30, 34],))
df['value'] = df['response'] + ' - ' + df['layout']
df = df.sort_values('value')
# df2 = df.groupby(['value', 'week']).sum().reset_index().sort_values('value')
fig = go.Figure()
for v in df['value'].unique():
dfp = df[df['value']==v]
fig.add_traces(go.Bar(x=dfp['week'], y = dfp['cnt'], name = v))
fig.update_layout(barmode='stack', template='plotly_dark')
fig.show()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
65 次 |
| 最近记录: |