# Turning on inline plots -- just for use in ipython notebooks. %pylab inline import matplotlib print matplotlib.__version__ print matplotlib.get_backend() import matplotlib.pyplot as plt ================== =========================================================================================== Function Description ================== =========================================================================================== `autoscale` Autoscale the axis view to the data (toggle). `axes` Add an axes to the figure. `axis` Convenience method to get or set axis properties. `cla` Clear the current axes. `clf` Clear the current figure. `clim` Set the color limits of the current image. `delaxes` Remove an axes from the current figure. `locator_params` Control behavior of tick locators. `margins` Set or retrieve autoscaling margins. `figure` Creates a new figure. `gca` Return the current axis instance. `gcf` Return a reference to the current figure. `gci` Get the current colorable artist. `hold` Set the hold state. `ioff` Turn interactive mode off. `ion` Turn interactive mode on. `ishold` Return the hold status of the current axes. `isinteractive` Return status of interactive mode. `rc` Set the current rc params. `rc_context` Return a context manager for managing rc settings. `rcdefaults` Restore the default rc params. `savefig` Save the current figure. `sca` Set the current Axes instance to *ax*. `sci` Set the current image. `set_cmap` Set the default colormap. `setp` Set a property on an artist object. `show` Display a figure. `subplot` Return a subplot axes positioned by the given grid definition. `subplot2grid` Create a subplot in a grid. `subplot_tool` Launch a subplot tool window for a figure. `subplots` Create a figure with a set of subplots already made. `subplots_adjust` Tune the subplot layout. `switch_backend` Switch the default backend. `tick_params` Change the appearance of ticks and tick labels. `ticklabel_format` Change the `~matplotlib.ticker.ScalarFormatter` used by default for linear axes. `tight_layout` Automatically adjust subplot parameters to give specified padding. `xkcd` Turns on `xkcd `_ sketch-style drawing mode. `xlabel` Set the *x* axis label of the current axis. `xlim` Get or set the *x* limits of the current axes. `xscale` Set the scaling of the *x*-axis. `xticks` Get or set the *x*-limits of the current tick locations and labels. `ylabel` Set the *y* axis label of the current axis. `ylim` Get or set the *y*-limits of the current axes. `yscale` Set the scaling of the *y*-axis. `yticks` Get or set the *y*-limits of the current tick locations and labels. ================== ============================================================================================================= =========================================================================================== Function Description ================== =========================================================================================== `acorr` Plot the autocorrelation of *x*. `bar` Make a bar plot. `barbs` Plot a 2-D field of barbs. `barh` Make a horizontal bar plot. `boxplot` Make a box and whisker plot. `broken_barh` Plot horizontal bars. `cohere` Plot the coherence between *x* and *y*. `contour` Plot contours. `contourf` Plot filled contours. `csd` Plot cross-spectral density. `errorbar` Plot an errorbar graph. `eventplot` Plot identical parallel lines at specific positions. `fill` Plot filled polygons. `fill_between` Make filled polygons between two curves. `fill_betweenx` Make filled polygons between two horizontal curves. `hexbin` Make a hexagonal binning plot. `hist` Plot a histogram. `hist2d` Make a 2D histogram plot. `imshow` Display an image on the axes. `loglog` Make a plot with log scaling on both the *x* and *y* axis. `matshow` Display an array as a matrix in a new figure window. `pcolor` Create a pseudocolor plot of a 2-D array. `pcolormesh` Plot a quadrilateral mesh. `pie` Plot a pie chart. `plot` Plot lines and/or markers to the :class:`~matplotlib.axes.Axes`. `plot_date` Plot with data with dates. `polar` Make a polar plot. `psd` Plot the power spectral density. `quiver` Plot a 2-D field of arrows. `scatter` Make a scatter plot of x vs y, where x and y are sequence like objects of the same lengths. `semilogx` Make a plot with log scaling on the *x* axis. `semilogy` Make a plot with log scaling on the *y* axis. `specgram` Plot a spectrogram. `spy` Plot the sparsity pattern on a 2-D array. `stackplot` Draws a stacked area plot. `stem` Create a stem plot. `step` Make a step plot. `streamplot` Draws streamlines of a vector flow. `tricontour` Draw contours on an unstructured triangular grid. `tricontourf` Draw contours on an unstructured triangular grid. `tripcolor` Create a pseudocolor plot of an unstructured triangular grid. `triplot` Draw a unstructured triangular grid as lines and/or markers. `xcorr` Plot the cross-correlation between *x* and *y*.================== =========================================================================================== Function Description ================== =========================================================================================== `annotate` Create an annotation: a piece of text referring to a data point. `arrow` Add an arrow to the axes. `axhline` Add a horizontal line across the axis. `axhspan` Add a horizontal span (rectangle) across the axis. `axvline` Add a vertical line across the axes. `axvspan` Add a vertical span (rectangle) across the axes. `box` Turn the axes box on or off. `clabel` Label a contour plot. `colorbar` Add a colorbar to a plot. `grid` Turn the axes grids on or off. `hlines` Plot horizontal lines. `legend` Place a legend on the current axes. `minorticks_off` Remove minor ticks from the current plot. `minorticks_on` Display minor ticks on the current plot. `quiverkey` Add a key to a quiver plot. `rgrids` Get or set the radial gridlines on a polar plot. `suptitle` Add a centered title to the figure. `table` Add a table to the current axes. `text` Add text to the axes. `title` Set a title of the current axes. `vlines` Plot vertical lines. `xlabel` Set the *x* axis label of the current axis. `ylabel` Set the *y* axis label of the current axis. ================== =========================================================================================== fig = plt.figure() fig = plt.figure(figsize=(10, 4)) fig.gca() # needed for the pylab-inline to display anything plt.show() fig = plt.figure(figsize=plt.figaspect(2.0)) # Twice as tall 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() fig, ax = plt.subplots(1, 1) ax.set_ylim( ) plt.show() 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" 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() # Without "tight_layout" 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.show() # With "tight_layout" 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() 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() fig = plt.figure() ax = fig.add_subplot(111) ax.plot([1, 2, 3, 4], [10, 20, 25, 30]) 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 ax.spines['bottom'].set_position(('outward', 10)) ax.spines['left'].set_position(('outward', 10)) plt.show() import numpy as np 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()