I am working with matplotlib to generate plots. Currently, I am trying to plot something with a broken secondary x-axis. I read the instructions like this one here: Python/Matplotlib - Is there a way to make a discontinuous axis? and tried to apply them to my problem. However, it looks like it's only possible to correctly plot the labels of the first, but not the secondary axis. Here is my code:
x=numpy.linspace(1, 25, 25)
y=x**2
# for a broken axis
fig,(ax,ax_2) = plt.subplots(1,2,sharey=True, facecolor='w')
ax.plot(x, y, "r^-")
ax.set_xlabel(r"oscillation range [$^\circ$]", **axis_font)
ax.set_ylabel(r"I/$\sigma$ [a.u.]", **axis_font)
fig.text(0.9, 0.055, r'x360$^\circ$', **axis_font)
# secondary x-axis
ax2 = ax.twiny()
dlim=9.04
_d1=[1.02, 1.7, 2.55, 3.4, 4.25]
_d2=numpy.linspace(5.12, 8.6, num=3)
ax2.set_xlabel("dose [MGy]", labelpad=10, **axis_font)
# setting ticks first x-axis
ax.set_xlim(0.5, 20.5)
ax_2.set_xlim(21, 25.5)
x2=[21, 23, 25]
ax_2.set_xticks(x2)
# setting ticks secondary x-axis
ax2.set_xticks(_d1)
ax2.set_xlim(0.5*0.85, dlim) # I should somehow be able to split the second x-axis, but how?
ax.spines['right'].set_visible(False)
ax_2.spines['left'].set_visible(False)
ax.yaxis.tick_left()
ax.tick_params(labelright='off')
ax_2.yaxis.tick_right()
if key.split("/")!=[]:
key=key.split("/")[0]
for item in (ax.get_xticklabels() + ax2.get_xticklabels()+
ax.get_yticklabels()+ ax_2.get_xticklabels()):
item.set_fontsize(tick_size)
item.set_weight(tick_weight)
pylab.show()
So, does anybody know a way to split the secondary axis, how to avoid that there is a secondary y-axis (I have no idea where it's coming from), how to obtain the break of the axis at 4/5th of the plot, how to continue the plot in the second half of the x-axis, and how to obtain the labels of the x-axis in the middle of the plot? Ideally, also only the secondary x-axis would be broken, and not the first...
Any advice would be very appreciated!