## matplotlib-checkins

 SF.net SVN: matplotlib:[8770] branches/v1_0_maint/doc/users/recipes.rst From: - 2010-11-04 20:50:56 Revision: 8770 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8770&view=rev Author: jdh2358 Date: 2010-11-04 20:50:49 +0000 (Thu, 04 Nov 2010) Log Message: ----------- some updates to recipes Modified Paths: -------------- branches/v1_0_maint/doc/users/recipes.rst Modified: branches/v1_0_maint/doc/users/recipes.rst =================================================================== --- branches/v1_0_maint/doc/users/recipes.rst 2010-11-04 18:28:03 UTC (rev 8769) +++ branches/v1_0_maint/doc/users/recipes.rst 2010-11-04 20:50:49 UTC (rev 8770) @@ -8,6 +8,66 @@ that illustrate some of the useful idioms and tricks to make snazzier figures and overcome some matplotlib warts. + +Sharing axis limits and views +============================= + +It's common to make two or more plots which share an axis, eg two +subplots with time as a common axis. When you pan and zoom around on +one, you want the other to move around with you. To facilitate this, +matplotlib Axes support a sharex and sharey attribute. When +you create a :func:~matplotlib.pyplot.subplot or +:func:~matplotlib.pyplot.axes instance, you can pass in a keyword +indicating what axes you want to share with + +.. sourcecode:: ipython + + In [96]: t = np.arange(0, 10, 0.01) + + In [97]: ax1 = plt.subplot(211) + + In [98]: ax1.plot(t, np.sin(2*np.pi*t)) + Out[98]: [] + + In [99]: ax2 = plt.subplot(212, sharex=ax1) + + In [100]: ax2.plot(t, np.sin(4*np.pi*t)) + Out[100]: [] + +Easily creating subplots +======================== + +In early versions of matplotlib, if you wanted to use the pythonic API +and create a figure instance and from that create a grid of subplots, +possibly with shared axes, it involved a fair amount of boilerplate +code. Eg + +.. sourcecode:: python + + # old style + fig = plt.figure() + ax1 = fig.add_subplot(221) + ax2 = fig.add_subplot(222, sharex=ax1, sharey=ax1) + ax3 = fig.add_subplot(223, sharex=ax1, sharey=ax1) + ax3 = fig.add_subplot(224, sharex=ax1, sharey=ax1) + +Fernando Perez has provided a nice top level method to create in +:func:~matplotlib.pyplots.subplots (note the "s" at the end) +everything at once, and turn off x and y sharing for the whole bunch. +You can either unpack the axes individually:: + + # new style method 1 + fig, (ax1, ax2, ax3, ax4) = plt.subplots(2, 2, sharex=True, sharey=True) + ax1.plot(x) + +or get them back as a numrows x numcolumns object array which supports +numpy indexing:: + + # new style method 2 + fig, axs = plt.subplots(2, 2, sharex=True, sharey=True) + axs[0,0].plot(x) + + Fixing common date annoyances ============================= @@ -23,7 +83,9 @@ In [64]: r = np.load(datafile).view(np.recarray) In [65]: r.dtype - Out[65]: dtype([('date', '|O4'), ('', '|V4'), ('open', '