要合并两个条形图

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

我有2个单独的条形图,我想尝试并结合起来,但似乎无法将它们绑在一起

combinedPassengerSimulation['StartStation'].value_counts().sort_index().plot(kind='bar', color="Green")
combinedPassengerSimulation['EndStation'].value_counts().sort_index().plot(kind='bar', color="Blue")

这些行分别执行时效果很好,但我确实希望能够将它们组合在一起,理想地显示在2号和3号车站到达和离开的乘客的交叉路口

任何帮助,非常感谢enter image description here

python pandas plotly
1个回答
0
投票

IIUC,您可以关注此

import pandas as pd
import numpy as np
import plotly.graph_objs as go
n = 100_000
df = pd.DataFrame({"StartStation":np.random.choice([1]*10+[2]*5+[3]*2, n),
                   "EndStation":np.random.choice([2]*4+[3]*7+[4]*2, n)})

# here you can use aggregate
out = df.agg({"StartStation": "value_counts",
              "EndStation": "value_counts"})

密谋

fig = go.Figure()
for col in out.columns:
    fig.add_trace(
        go.Bar(x=out.index,
               y=out[col],
               name=col))
fig.show()

enter image description here

Pandas / matplotlib

out.plot(kind='bar');

enter image description here

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