def main4(): x = np.arange(0.0, 12, 0.1) y = 0.1 * x * np.sin(x) fig = plt.figure(figsize=(6.8, 2.55), dpi=100) ax1 = fig.add_subplot(121) ax1.plot(x, y, color='blue', label='plot 1') ax1.hlines(y=0, xmin=0, xmax=30, linewidth=1) ax1.set_title('shift.yaxis(ax, 0, 0.2, True)') ax1.set_ylabel('Plot 1', color='blue') ax1.set_xlim(0.0, 12.0) shift.yaxis(ax1, 0, 0.2, True) ax2 = fig.add_subplot(122) ax2.plot(x, y, color='blue', label='plot 1') ax2.hlines(y=0, xmin=0, xmax=30, linewidth=1) ax2.set_title('shift.yaxis(ax, 0, 0.8, True)') ax2.set_ylabel('Plot 1', color='blue') ax2.set_xlim(0.0, 12.0) shift.yaxis(ax2, 0, 0.8, True) plt.tight_layout() plt.savefig("shift_plt4.png")
def test_yaxes_expand_simple(): fig.clear() ax = fig.add_subplot(111) ax.set_xlim(0.0, 1.0) org = 0.0 pos = 0.5 shift.yaxis(ax, org, pos, True) assert ax.get_ylim() == (-1.0, 1.0)
def yaxes(ax1, org1, ax2, org2, pos=None): """ Adjust the plotting range of two y axes to align the origins with the position. Parameters ---------- ax1 : matplotlib.axes.Axes First axis object of matplotlib. org1 : float Origin of first axis to be aligned. ax2 : matplotlib.axes.Axes Second axis object of matplotlib. org2 : float Origin of second axis to be aligned. pos : float or None, optional Relative position to align the origins [0 < pos < 1]. When pos is None, the origins are aligned to the middle of them. Returns ------- Raises ------ TypeError If 'ax1' and/or 'ax2' are not the Axes object of matplotlib. ValueError If 'pos' is less than or equal to 0, or more than or equal to 1. Examples -------- >>> import matplotlib.pyplot as plt >>> from mpl_axes_aligner import align >>> fig = plt.figure() >>> ax1 = fig.add_subplot(111) >>> ax2 = ax1.twinx() >>> ax1.set_ylim(-1.0, 0.0) (-1.0, 0.0) >>> ax2.set_ylim(0.0, 1.0) (0.0, 1.0) >>> align.yaxes(ax1, -0.3, ax2, 0.3, 0.5) >>> ax1.get_ylim() (-1.0, 0.4) >>> ax2.get_ylim() (-0.4, 1.0) """ if pos is None: # Get plotting ranges try: lim1 = list(ax1.get_ylim()) lim2 = list(ax2.get_ylim()) except AttributeError or TypeError: raise TypeError("'ax1' and 'ax2' should be Axes objects of " "matplotlib.") # Calculate the position pos = _calc_pos(org1, org2, lim1, lim2) elif pos <= 0 or pos >= 1: raise ValueError("The position to align the origins should be " "0 < pos < 1.") # Apply the new ranges shift.yaxis(ax1, org1, pos, True) shift.yaxis(ax2, org2, pos, True)
def test_yaxis_shift_ValueError(pos): fig.clear() ax = fig.add_subplot(111) org = 0.5 with pytest.raises(ValueError): shift.yaxis(ax, org, pos)
def test_yaxis_shift_NoError(pos): fig.clear() ax = fig.add_subplot(111) org = 0.5 shift.yaxis(ax, org, pos)