# Turning on inline plots -- just for use in ipython notebooks. %matplotlib inline import matplotlib print matplotlib.__version__ print matplotlib.get_backend() import numpy as np import matplotlib.pyplot as plt fig = plt.figure() fig = plt.figure(figsize=(10, 4)) fig.gca() # needed for the ipython-inline to display anything plt.show() # Twice as tall fig = plt.figure(figsize=plt.figaspect(2.0)) fig.gca() plt.show() fig = plt.figure() ax = fig.add_subplot(111) ax.plot([1, 2, 3, 4], [10, 20, 25, 30]) ax.set_xlim(0.5, 4.5) plt.show() plt.plot([1, 2, 3, 4], [10, 20, 25, 30]) plt.xlim(0.5, 4.5) plt.show() fig, (ax1, ax2) = plt.subplots(1, 2, figsize=plt.figaspect(0.5)) ax1.plot([-10, -5, 0, 5, 10, 15], [-1.2, 2, 3.5, -0.3, -4, 1]) ax2.scatter([-10, -5, 0, 5, 10, 15], [-1.2, 2, 3.5, -0.3, -4, 1]) plt.show() # Good -- setting limits after plotting is done fig, (ax1, ax2) = plt.subplots(1, 2, figsize=plt.figaspect(0.5)) ax1.plot([-10, -5, 0, 5, 10, 15], [-1.2, 2, 3.5, -0.3, -4, 1]) ax2.scatter([-10, -5, 0, 5, 10, 15], [-1.2, 2, 3.5, -0.3, -4, 1]) ax1.set_ylim(bottom=-10) ax2.set_xlim(right=25) plt.show() # Bad -- Setting limits before plotting is done fig, (ax1, ax2) = plt.subplots(1, 2, figsize=plt.figaspect(0.5)) ax1.set_ylim(bottom=-10) ax2.set_xlim(right=25) ax1.plot([-10, -5, 0, 5, 10, 15], [-1.2, 2, 3.5, -0.3, -4, 1]) ax2.scatter([-10, -5, 0, 5, 10, 15], [-1.2, 2, 3.5, -0.3, -4, 1]) plt.show() %load exercises/1.1-limits.py fig = plt.figure() ax = fig.add_subplot(111) ax.plot([1, 2, 3, 4], [10, 20, 25, 30], label='Philadelphia') ax.plot([1, 2, 3, 4], [30, 23, 13, 4], label='Boston') ax.set_ylabel('Temperature (deg C)') ax.set_xlabel('Time') ax.set_title("A tale of two cities") ax.legend() plt.show() fig, ax = plt.subplots(1, 1) ax.bar([1, 2, 3, 4], [10, 20, 25, 30], label="Foobar") ax.plot([1, 2, 3, 4], [10, 20, 25, 30], label="_nolegend_") ax.legend() plt.show() fig = plt.figure() ax = fig.add_subplot(111) ax.plot([1, 2, 3, 4], [10, 20, 25, 30]) ax.xaxis.set_ticks(range(1, 5)) # Set ticks at 1, 2, 3, 4 ax.xaxis.set_ticklabels([3, 100, -12, "foo"]) # Label ticks as "3", "100", "-12", and "foo" ax.tick_params(axis='y', direction='inout', length=10) plt.show() fig = plt.figure(figsize=(10, 5)) ax = fig.add_subplot(121) ax.plot([1, 2, 3, 4], [10, 20, 25, 30], label='Philadelphia') ax.plot([1, 2, 3, 4], [30, 23, 13, 4], label='Boston') ax.set_title('A tale of two cities') ax.legend() t = np.linspace(0, 7, 25) z = 2 * np.sin(t) + 5 ax = fig.add_subplot(122) ax.scatter(t, z, label='Philadelphia') ax.set_title("Observed Tide") ax.legend() fig.suptitle('A title for the whole figure') plt.show() fig, axes = plt.subplots(2, 2, figsize=(9, 9)) plt.subplots_adjust(wspace=0.5, hspace=0.3, left=0.125, right=0.9, top=0.9, bottom=0.1) plt.show() def example_plot(ax): ax.plot([1, 2]) ax.set_xlabel('x-label', fontsize=16) ax.set_ylabel('y-label', fontsize=8) ax.set_title('Title', fontsize=24) fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2) example_plot(ax1) example_plot(ax2) example_plot(ax3) example_plot(ax4) #plt.tight_layout() plt.show() # going out of inline mode to demonstrate the zooming and panning %matplotlib fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True) ax1.plot([1, 2, 3, 4], [1, 2, 3, 4]) ax2.plot([3, 4, 5, 6], [6, 5, 4, 3]) plt.show() # Go back to inline mode %matplotlib inline fig, ax1 = plt.subplots(1, 1) ax1.plot([1, 2, 3, 4], [1, 2, 3, 4]) ax2 = ax1.twinx() ax2.scatter([1, 2, 3, 4], [60, 50, 40, 30]) ax1.set_xlabel('X') ax1.set_ylabel('First scale') ax2.set_ylabel('Other scale') plt.show() fig, ax = plt.subplots(1, 1) ax.plot([-2, 2, 3, 4], [-10, 20, 25, 5]) ax.spines['top'].set_visible(False) ax.xaxis.set_ticks_position('bottom') # no ticklines at the top ax.spines['right'].set_visible(False) ax.yaxis.set_ticks_position('left') # no ticklines on the right # "outward" # Move the two remaining spines "out" away from the plot by 10 points ax.spines['bottom'].set_position(('outward', 10)) ax.spines['left'].set_position(('outward', 10)) # "data" # Have the spines stay intersected at (0,0) #ax.spines['bottom'].set_position(('data', 0)) #ax.spines['left'].set_position(('data', 0)) # "axes" # Have the two remaining spines placed at a fraction of the axes #ax.spines['bottom'].set_position(('axes', 0.75)) #ax.spines['left'].set_position(('axes', 0.25)) plt.show() %load exercises/1.2-spines.py y, x = np.ogrid[-6:6:20j, -10:10:30j] z = np.hypot(x, y) plt.imshow(z) plt.colorbar() plt.show() plt.imshow(z) plt.colorbar(orientation='horizontal', shrink=0.75) # We can make colorbars do all sorts of things! plt.show() plt.imshow(z) cbar = plt.colorbar(extend='both', aspect=10) cbar.set_label('distance') # And we can even add a label to it plt.show() fig, (ax1, ax2) = plt.subplots(1, 2) ax1.imshow(z) im = ax2.imshow(z) # Note, due to a bug, you will need to save the # returned image object when calling imshow() from an Axes # and pass that to plt.colorbar() so that it knows what # image to build a colorbar from. This will be fixed for v1.3.1. plt.colorbar(im) plt.show() fig, (ax1, ax2) = plt.subplots(1, 2) ax1.imshow(z) im = ax2.imshow(z) plt.colorbar(im, ax=[ax1, ax2], shrink=0.5) plt.show()