I'm trying to do a barplot with python's matplotlib that has a broken y-axis to represent an outlier. But due to aesthetics, I need the bar of the outlier to be continuous, not to be broken. Any idea on how I can do this would be really appreciated. Reducing the gap between subplots is not an option.
I've learned how to do broken-axis plots using matplotlib docs but I cannot find anything like I need to do
This is an example code
# %% create a broken-axis barplot
import matplotlib.pyplot as plt
import numpy as np
# create fake data
x = np.arange(1, 6)
y = np.exp(x)
# add outlier
y[2] = 15000
# bar plot with broken axis
f, (ax, ax2) = plt.subplots(2,1, sharex=True, facecolor='w')
ax.bar(x, y)
ax2.bar(x, y)
# set limits
ax2.set_ylim(0, 100)
ax.set_ylim(10000, 20000)
# hide the spines between ax and ax2
ax.spines['bottom'].set_visible(False)
ax2.spines['top'].set_visible(False)
# hide ticks
ax.xaxis.tick_top()
plt.savefig('broken-axis-barplot.png', dpi=100, bbox_inches='tight')
Basically bar 3 needs to be filled up between the two chunks
In your code, you have the ax
which has data till 100 (the lower limit). To extend the bar outside the plot area, you can add , clip_on=False
while plotting that bar, which will extend the bar outside the area of the first plot. So, basically, changing that line to...
ax.bar(x, y, clip_on=False)
...gives you the below plot