Pandas: Bar-Plot with two bars and two y-axis

后端 未结 4 979
清酒与你
清酒与你 2020-11-30 23:21

I have a DataFrame looking like this:

     amount     price
age
A     40929   4066443
B     93904   9611272
C    188349  19360005
D    248438  24335536
E             


        
4条回答
  •  暖寄归人
    2020-11-30 23:23

    Here is an other method:

    • create all the bars in left axes
    • move some bars to the right axes by change it's transform attribute

    Here is the code:

    import pylab as pl
    df = pd.DataFrame(np.random.rand(10, 2), columns=["left", "right"])
    df["left"] *= 100
    
    ax = df.plot(kind="bar")
    ax2 = ax.twinx()
    for r in ax.patches[len(df):]:
        r.set_transform(ax2.transData)
    ax2.set_ylim(0, 2);
    

    here is the output:

    enter image description here

提交回复
热议问题