|  | 
| 9 | 9 | import numpy as np | 
| 10 | 10 | import matplotlib.pyplot as plt | 
| 11 | 11 | 
 | 
| 12 |  | -plt.subplots_adjust(hspace=0.4) | 
|  | 12 | +# Data for plotting | 
| 13 | 13 | t = np.arange(0.01, 20.0, 0.01) | 
| 14 | 14 | 
 | 
|  | 15 | +# Create figure | 
|  | 16 | +fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2) | 
|  | 17 | + | 
|  | 18 | +# Note hspace is the amount of height reserved for white space between subplots | 
|  | 19 | +# expressed as a fraction of the average axis height | 
|  | 20 | +fig.subplots_adjust(hspace=0.5) | 
|  | 21 | + | 
| 15 | 22 | # log y axis | 
| 16 |  | -plt.subplot(221) | 
| 17 |  | -plt.semilogy(t, np.exp(-t/5.0)) | 
| 18 |  | -plt.title('semilogy') | 
| 19 |  | -plt.grid(True) | 
|  | 23 | +ax1.semilogy(t, np.exp(-t/5.0)) | 
|  | 24 | +ax1.set(title='semilogy') | 
|  | 25 | +ax1.grid() | 
| 20 | 26 | 
 | 
| 21 | 27 | # log x axis | 
| 22 |  | -plt.subplot(222) | 
| 23 |  | -plt.semilogx(t, np.sin(2*np.pi*t)) | 
| 24 |  | -plt.title('semilogx') | 
| 25 |  | -plt.grid(True) | 
|  | 28 | +ax2.semilogx(t, np.sin(2*np.pi*t)) | 
|  | 29 | +ax2.set(title='semilogx') | 
|  | 30 | +ax2.grid() | 
| 26 | 31 | 
 | 
| 27 | 32 | # log x and y axis | 
| 28 |  | -plt.subplot(223) | 
| 29 |  | -plt.loglog(t, 20*np.exp(-t/10.0), basex=2) | 
| 30 |  | -plt.grid(True) | 
| 31 |  | -plt.title('loglog base 2 on x') | 
| 32 |  | - | 
| 33 |  | -# with errorbars: clip non-positive values | 
| 34 |  | -ax = plt.subplot(224) | 
| 35 |  | -ax.set_xscale("log", nonposx='clip') | 
| 36 |  | -ax.set_yscale("log", nonposy='clip') | 
|  | 33 | +ax3.loglog(t, 20*np.exp(-t/10.0), basex=2) | 
|  | 34 | +ax3.set(title='loglog base 2 on x') | 
|  | 35 | +ax3.grid() | 
| 37 | 36 | 
 | 
|  | 37 | +# With errorbars: clip non-positive values | 
|  | 38 | +# Use new data for plotting | 
| 38 | 39 | x = 10.0**np.linspace(0.0, 2.0, 20) | 
| 39 | 40 | y = x**2.0 | 
| 40 |  | -plt.errorbar(x, y, xerr=0.1*x, yerr=5.0 + 0.75*y) | 
| 41 |  | -ax.set_ylim(ymin=0.1) | 
| 42 |  | -ax.set_title('Errorbars go negative') | 
| 43 | 41 | 
 | 
|  | 42 | +ax4.set_xscale("log", nonposx='clip') | 
|  | 43 | +ax4.set_yscale("log", nonposy='clip') | 
|  | 44 | +ax4.set(title='Errorbars go negative') | 
|  | 45 | +ax4.errorbar(x, y, xerr=0.1*x, yerr=5.0 + 0.75*y) | 
|  | 46 | +# ylim must be set after errorbar to allow errorbar to autoscale limits | 
|  | 47 | +ax4.set_ylim(ymin=0.1) | 
| 44 | 48 | 
 | 
| 45 | 49 | plt.show() | 
0 commit comments