简体   繁体   中英

Matplotlib : How to plot two bar plots with the same x/y axes but one start over the other along y axis

I'm trying to plot two bar plots int the same figure. In the code below the plots are one behind the other because they laid along y axis at 0.

import matplotlib.pyplot as plt
import numpy as np

y1=np.array([1,2,3,4,5])
x1=np.array([1,2,3,2,1])

x2=np.array([1,2,3,2,1])
y2=np.array([6,7,8,9,10])
plt.bar(y1,x1)
plt.bar(y2,x2)

在此处输入图像描述

I would the orange bar plot starts from coordinates: (x=6, y=3). So orange bar plot should be over the blue plot and in the same figure.

Just provide the value of the bottom for the second bar plot using bottom=3

plt.bar(y2,x2, bottom=3)

You can also make it more general using bottom=max(x2)

在此处输入图像描述

For horizontal bar chart Use left=3

plt.barh(y2,x2,left=3)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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