简体   繁体   中英

Matplotlib: x-axis break with bar plot, whilst keeping scale the same

I wish to plot a simple bar chart where there is an axis break between the numbers 5-9 on the x-axis. However, I want the width of the bars and the scale to be consistent. Here, I am having problems with both the axis break markers, and getting my data into the subplots. I have tried:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

x = [2,3,4,10]
y = [-10, -20, -25, -50]

f,(ax,ax2) = plt.subplots(1,2,sharey=True, facecolor='w')
gs = gridspec.GridSpec(1, 2,width_ratios=[2,1])
ax.bar(x, y)
ax2.bar(x, y)
ax = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])

ax.set_xlim(1.5,4.5)
ax2.set_xlim(9.5,10.5)

ax.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)

ax.yaxis.tick_left()
ax2.yaxis.tick_right()
ax.set_xticks([2,3,4])
ax2.set_xticks([10])

ax.set_ylabel("Y-Axis)")
f.text(0.5, 0.04, "X-Axis", ha="center")

d = .015
kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)
ax.plot((1-d,1+d), (-d,+d), **kwargs)
ax.plot((1-d,1+d),(1-d,1+d), **kwargs)

kwargs.update(transform=ax2.transAxes)
ax2.plot((-d,+d), (1-d,1+d), **kwargs)
ax2.plot((-d,+d), (-d,+d), **kwargs)

plt.savefig("leaf.png", format="png", dpi=1000, bbox_inches="tight")

However, this method does not include my data (probably because I am overwriting ax and ax2, I end up getting unaligned error bars, and I get tick markers on the right hand side of the second graph.

在此处输入图像描述

However, if I comment out plt.subplot(gs[0]), plt.subplot(gs[1]) the size of the bars becomes very different as the scale changes.

在此处输入图像描述

A lot of the other examples where a break is given in an axis either show different scales or place the break exactly in the middle of the series.

The method of breaking the x-axis was difficult with surprisingly few examples. The width of the bar graph was automatically extended, so getting the width of the bar graph after creating the graph remained at the default value. There may be a way to deal with this if the current bar chart width can be retrieved. So my way to deal with this was to change the x-axis limit to the same spacing as the first and change the graph size ratio for the subplots. I would like to make the spacing a bit closer, but it will take some trial and error.

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

x = [2,3,4,10]
y = [-10, -20, -25, -50]

f,(ax,ax2) = plt.subplots(1,2, sharey=True, facecolor='w', gridspec_kw={'width_ratios': [2, 1]})
ax.bar(x, y)
ax2.bar(x, y)

ax.set_xlim(1.5, 4.5)
ax2.set_xlim(9.0, 10.5)

ax.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)

ax.yaxis.tick_left()
ax2.yaxis.tick_right()
ax.set_xticks([2,3,4])
ax2.set_xticks([10])

ax.set_ylabel("Y-Axis)")
f.text(0.5, 0.04, "X-Axis", ha="center")

d = .015
kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)
ax.plot((1-d,1+d), (-d,+d), **kwargs)
ax.plot((1-d,1+d),(1-d,1+d), **kwargs)

kwargs.update(transform=ax2.transAxes)
ax2.plot((-d,+d), (1-d,1+d), **kwargs)
ax2.plot((-d,+d), (-d,+d), **kwargs)

#plt.savefig("leaf.png", format="png", dpi=1000, bbox_inches="tight")
plt.show()

在此处输入图像描述

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