diff --git a/examples/pylab_examples/ganged_plots.py b/examples/pylab_examples/ganged_plots.py deleted file mode 100644 index 9d14a522c546..000000000000 --- a/examples/pylab_examples/ganged_plots.py +++ /dev/null @@ -1,45 +0,0 @@ -""" -To create plots that share a common axes (visually) you can set the -hspace between the subplots close to zero (do not use zero itself). -Normally you'll want to turn off the tick labels on all but one of the -axes. - -In this example the plots share a common xaxis but you can follow the -same logic to supply a common y axis. -""" -import matplotlib.pyplot as plt -import numpy as np - -t = np.arange(0.0, 2.0, 0.01) - -s1 = np.sin(2*np.pi*t) -s2 = np.exp(-t) -s3 = s1*s2 - -# axes rect in relative 0,1 coords left, bottom, width, height. Turn -# off xtick labels on all but the lower plot - - -f = plt.figure() -plt.subplots_adjust(hspace=0.001) - - -ax1 = plt.subplot(311) -ax1.plot(t, s1) -plt.yticks(np.arange(-0.9, 1.0, 0.4)) -plt.ylim(-1, 1) - -ax2 = plt.subplot(312, sharex=ax1) -ax2.plot(t, s2) -plt.yticks(np.arange(0.1, 1.0, 0.2)) -plt.ylim(0, 1) - -ax3 = plt.subplot(313, sharex=ax1) -ax3.plot(t, s3) -plt.yticks(np.arange(-0.9, 1.0, 0.4)) -plt.ylim(-1, 1) - -xticklabels = ax1.get_xticklabels() + ax2.get_xticklabels() -plt.setp(xticklabels, visible=False) - -plt.show() diff --git a/examples/subplots_axes_and_figures/ganged_plots.py b/examples/subplots_axes_and_figures/ganged_plots.py new file mode 100644 index 000000000000..4014dabdf046 --- /dev/null +++ b/examples/subplots_axes_and_figures/ganged_plots.py @@ -0,0 +1,40 @@ +""" +========================== +Creating adjacent subplots +========================== + +To create plots that share a common axis (visually) you can set the hspace +between the subplots to zero. Passing sharex=True when creating the subplots +will automatically turn off all x ticks and labels except those on the bottom +axis. + +In this example the plots share a common x axis but you can follow the same +logic to supply a common y axis. +""" +import matplotlib.pyplot as plt +import numpy as np + +t = np.arange(0.0, 2.0, 0.01) + +s1 = np.sin(2 * np.pi * t) +s2 = np.exp(-t) +s3 = s1 * s2 + +fig, axs = plt.subplots(3, 1, sharex=True) +# Remove horizontal space between axes +fig.subplots_adjust(hspace=0) + +# Plot each graph, and manually set the y tick values +axs[0].plot(t, s1) +axs[0].set_yticks(np.arange(-0.9, 1.0, 0.4)) +axs[0].set_ylim(-1, 1) + +axs[1].plot(t, s2) +axs[1].set_yticks(np.arange(0.1, 1.0, 0.2)) +axs[1].set_ylim(0, 1) + +axs[2].plot(t, s3) +axs[2].set_yticks(np.arange(-0.9, 1.0, 0.4)) +axs[2].set_ylim(-1, 1) + +plt.show()