Skip to content

Commit 6127608

Browse files
committed
Updating docs for v3.1.1
1 parent 75292e8 commit 6127608

File tree

12,875 files changed

+1095560
-23716
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

12,875 files changed

+1095560
-23716
lines changed

3.1.1/Matplotlib.pdf

21 MB
Binary file not shown.
Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"collapsed": false
8+
},
9+
"outputs": [],
10+
"source": [
11+
"%matplotlib inline"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"\n# Customizing Figure Layouts Using GridSpec and Other Functions\n\n\nHow to create grid-shaped combinations of axes.\n\n :func:`~matplotlib.pyplot.subplots`\n Perhaps the primary function used to create figures and axes.\n It's also similar to :func:`.matplotlib.pyplot.subplot`,\n but creates and places all axes on the figure at once. See also\n `matplotlib.Figure.subplots`.\n\n :class:`~matplotlib.gridspec.GridSpec`\n Specifies the geometry of the grid that a subplot will be\n placed. The number of rows and number of columns of the grid\n need to be set. Optionally, the subplot layout parameters\n (e.g., left, right, etc.) can be tuned.\n\n :class:`~matplotlib.gridspec.SubplotSpec`\n Specifies the location of the subplot in the given *GridSpec*.\n\n :func:`~matplotlib.pyplot.subplot2grid`\n A helper function that is similar to\n :func:`~matplotlib.pyplot.subplot`,\n but uses 0-based indexing and let subplot to occupy multiple cells.\n This function is not covered in this tutorial.\n"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"metadata": {
25+
"collapsed": false
26+
},
27+
"outputs": [],
28+
"source": [
29+
"import matplotlib\nimport matplotlib.pyplot as plt\nimport matplotlib.gridspec as gridspec"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"Basic Quickstart Guide\n======================\n\nThese first two examples show how to create a basic 2-by-2 grid using\nboth :func:`~matplotlib.pyplot.subplots` and :mod:`~matplotlib.gridspec`.\n\nUsing :func:`~matplotlib.pyplot.subplots` is quite simple.\nIt returns a :class:`~matplotlib.figure.Figure` instance and an array of\n:class:`~matplotlib.axes.Axes` objects.\n\n"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"metadata": {
43+
"collapsed": false
44+
},
45+
"outputs": [],
46+
"source": [
47+
"fig1, f1_axes = plt.subplots(ncols=2, nrows=2, constrained_layout=True)"
48+
]
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"metadata": {},
53+
"source": [
54+
"For a simple use case such as this, :mod:`~matplotlib.gridspec` is\nperhaps overly verbose.\nYou have to create the figure and :class:`~matplotlib.gridspec.GridSpec`\ninstance separately, then pass elements of gridspec instance to the\n:func:`~matplotlib.figure.Figure.add_subplot` method to create the axes\nobjects.\nThe elements of the gridspec are accessed in generally the same manner as\nnumpy arrays.\n\n"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": null,
60+
"metadata": {
61+
"collapsed": false
62+
},
63+
"outputs": [],
64+
"source": [
65+
"fig2 = plt.figure(constrained_layout=True)\nspec2 = gridspec.GridSpec(ncols=2, nrows=2, figure=fig2)\nf2_ax1 = fig2.add_subplot(spec2[0, 0])\nf2_ax2 = fig2.add_subplot(spec2[0, 1])\nf2_ax3 = fig2.add_subplot(spec2[1, 0])\nf2_ax4 = fig2.add_subplot(spec2[1, 1])"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {},
71+
"source": [
72+
"The power of gridspec comes in being able to create subplots that span\nrows and columns. Note the\n`Numpy slice <https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html>`_\nsyntax for selecting the part of the gridspec each subplot will occupy.\n\nNote that we have also used the convenience method `.Figure.add_gridspec`\ninstead of `.gridspec.GridSpec`, potentially saving the user an import,\nand keeping the namespace cleaner.\n\n"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"metadata": {
79+
"collapsed": false
80+
},
81+
"outputs": [],
82+
"source": [
83+
"fig3 = plt.figure(constrained_layout=True)\ngs = fig3.add_gridspec(3, 3)\nf3_ax1 = fig3.add_subplot(gs[0, :])\nf3_ax1.set_title('gs[0, :]')\nf3_ax2 = fig3.add_subplot(gs[1, :-1])\nf3_ax2.set_title('gs[1, :-1]')\nf3_ax3 = fig3.add_subplot(gs[1:, -1])\nf3_ax3.set_title('gs[1:, -1]')\nf3_ax4 = fig3.add_subplot(gs[-1, 0])\nf3_ax4.set_title('gs[-1, 0]')\nf3_ax5 = fig3.add_subplot(gs[-1, -2])\nf3_ax5.set_title('gs[-1, -2]')"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
":mod:`~matplotlib.gridspec` is also indispensable for creating subplots\nof different widths via a couple of methods.\n\nThe method shown here is similar to the one above and initializes a\nuniform grid specification,\nand then uses numpy indexing and slices to allocate multiple\n\"cells\" for a given subplot.\n\n"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": null,
96+
"metadata": {
97+
"collapsed": false
98+
},
99+
"outputs": [],
100+
"source": [
101+
"fig4 = plt.figure(constrained_layout=True)\nspec4 = fig4.add_gridspec(ncols=2, nrows=2)\nanno_opts = dict(xy=(0.5, 0.5), xycoords='axes fraction',\n va='center', ha='center')\n\nf4_ax1 = fig4.add_subplot(spec4[0, 0])\nf4_ax1.annotate('GridSpec[0, 0]', **anno_opts)\nfig4.add_subplot(spec4[0, 1]).annotate('GridSpec[0, 1:]', **anno_opts)\nfig4.add_subplot(spec4[1, 0]).annotate('GridSpec[1:, 0]', **anno_opts)\nfig4.add_subplot(spec4[1, 1]).annotate('GridSpec[1:, 1:]', **anno_opts)"
102+
]
103+
},
104+
{
105+
"cell_type": "markdown",
106+
"metadata": {},
107+
"source": [
108+
"Another option is to use the ``width_ratios`` and ``height_ratios``\nparameters. These keyword arguments are lists of numbers.\nNote that absolute values are meaningless, only their relative ratios\nmatter. That means that ``width_ratios=[2, 4, 8]`` is equivalent to\n``width_ratios=[1, 2, 4]`` within equally wide figures.\nFor the sake of demonstration, we'll blindly create the axes within\n``for`` loops since we won't need them later.\n\n"
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"execution_count": null,
114+
"metadata": {
115+
"collapsed": false
116+
},
117+
"outputs": [],
118+
"source": [
119+
"fig5 = plt.figure(constrained_layout=True)\nwidths = [2, 3, 1.5]\nheights = [1, 3, 2]\nspec5 = fig5.add_gridspec(ncols=3, nrows=3, width_ratios=widths,\n height_ratios=heights)\nfor row in range(3):\n for col in range(3):\n ax = fig5.add_subplot(spec5[row, col])\n label = 'Width: {}\\nHeight: {}'.format(widths[col], heights[row])\n ax.annotate(label, (0.1, 0.5), xycoords='axes fraction', va='center')"
120+
]
121+
},
122+
{
123+
"cell_type": "markdown",
124+
"metadata": {},
125+
"source": [
126+
"Learning to use ``width_ratios`` and ``height_ratios`` is particularly\nuseful since the top-level function :func:`~matplotlib.pyplot.subplots`\naccepts them within the ``gridspec_kw`` parameter.\nFor that matter, any parameter accepted by\n:class:`~matplotlib.gridspec.GridSpec` can be passed to\n:func:`~matplotlib.pyplot.subplots` via the ``gridspec_kw`` parameter.\nThis example recreates the previous figure without directly using a\ngridspec instance.\n\n"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": null,
132+
"metadata": {
133+
"collapsed": false
134+
},
135+
"outputs": [],
136+
"source": [
137+
"gs_kw = dict(width_ratios=widths, height_ratios=heights)\nfig6, f6_axes = plt.subplots(ncols=3, nrows=3, constrained_layout=True,\n gridspec_kw=gs_kw)\nfor r, row in enumerate(f6_axes):\n for c, ax in enumerate(row):\n label = 'Width: {}\\nHeight: {}'.format(widths[c], heights[r])\n ax.annotate(label, (0.1, 0.5), xycoords='axes fraction', va='center')"
138+
]
139+
},
140+
{
141+
"cell_type": "markdown",
142+
"metadata": {},
143+
"source": [
144+
"The ``subplots`` and ``gridspec`` methods can be combined since it is\nsometimes more convenient to make most of the subplots using ``subplots``\nand then remove some and combine them. Here we create a layout with\nthe bottom two axes in the last column combined.\n\n"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": null,
150+
"metadata": {
151+
"collapsed": false
152+
},
153+
"outputs": [],
154+
"source": [
155+
"fig7, f7_axs = plt.subplots(ncols=3, nrows=3)\ngs = f7_axs[1, 2].get_gridspec()\n# remove the underlying axes\nfor ax in f7_axs[1:, -1]:\n ax.remove()\naxbig = fig7.add_subplot(gs[1:, -1])\naxbig.annotate('Big Axes \\nGridSpec[1:, -1]', (0.1, 0.5),\n xycoords='axes fraction', va='center')\n\nfig7.tight_layout()"
156+
]
157+
},
158+
{
159+
"cell_type": "markdown",
160+
"metadata": {},
161+
"source": [
162+
"Fine Adjustments to a Gridspec Layout\n=====================================\n\nWhen a GridSpec is explicitly used, you can adjust the layout\nparameters of subplots that are created from the GridSpec. Note this\noption is not compatible with ``constrained_layout`` or\n`.Figure.tight_layout` which both adjust subplot sizes to fill the\nfigure.\n\n"
163+
]
164+
},
165+
{
166+
"cell_type": "code",
167+
"execution_count": null,
168+
"metadata": {
169+
"collapsed": false
170+
},
171+
"outputs": [],
172+
"source": [
173+
"fig8 = plt.figure(constrained_layout=False)\ngs1 = fig8.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48, wspace=0.05)\nf8_ax1 = fig8.add_subplot(gs1[:-1, :])\nf8_ax2 = fig8.add_subplot(gs1[-1, :-1])\nf8_ax3 = fig8.add_subplot(gs1[-1, -1])"
174+
]
175+
},
176+
{
177+
"cell_type": "markdown",
178+
"metadata": {},
179+
"source": [
180+
"This is similar to :func:`~matplotlib.pyplot.subplots_adjust`, but it only\naffects the subplots that are created from the given GridSpec.\n\nFor example, compare the left and right sides of this figure:\n\n"
181+
]
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": null,
186+
"metadata": {
187+
"collapsed": false
188+
},
189+
"outputs": [],
190+
"source": [
191+
"fig9 = plt.figure(constrained_layout=False)\ngs1 = fig9.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48,\n wspace=0.05)\nf9_ax1 = fig9.add_subplot(gs1[:-1, :])\nf9_ax2 = fig9.add_subplot(gs1[-1, :-1])\nf9_ax3 = fig9.add_subplot(gs1[-1, -1])\n\ngs2 = fig9.add_gridspec(nrows=3, ncols=3, left=0.55, right=0.98,\n hspace=0.05)\nf9_ax4 = fig9.add_subplot(gs2[:, :-1])\nf9_ax5 = fig9.add_subplot(gs2[:-1, -1])\nf9_ax6 = fig9.add_subplot(gs2[-1, -1])"
192+
]
193+
},
194+
{
195+
"cell_type": "markdown",
196+
"metadata": {},
197+
"source": [
198+
"GridSpec using SubplotSpec\n==========================\n\nYou can create GridSpec from the :class:`~matplotlib.gridspec.SubplotSpec`,\nin which case its layout parameters are set to that of the location of\nthe given SubplotSpec.\n\nNote this is also available from the more verbose\n`.gridspec.GridSpecFromSubplotSpec`.\n\n"
199+
]
200+
},
201+
{
202+
"cell_type": "code",
203+
"execution_count": null,
204+
"metadata": {
205+
"collapsed": false
206+
},
207+
"outputs": [],
208+
"source": [
209+
"fig10 = plt.figure(constrained_layout=True)\ngs0 = fig10.add_gridspec(1, 2)\n\ngs00 = gs0[0].subgridspec(2, 3)\ngs01 = gs0[1].subgridspec(3, 2)\n\nfor a in range(2):\n for b in range(3):\n fig10.add_subplot(gs00[a, b])\n fig10.add_subplot(gs01[b, a])"
210+
]
211+
},
212+
{
213+
"cell_type": "markdown",
214+
"metadata": {},
215+
"source": [
216+
"A Complex Nested GridSpec using SubplotSpec\n===========================================\n\nHere's a more sophisticated example of nested GridSpec where we put\na box around each cell of the outer 4x4 grid, by hiding appropriate\nspines in each of the inner 3x3 grids.\n\n"
217+
]
218+
},
219+
{
220+
"cell_type": "code",
221+
"execution_count": null,
222+
"metadata": {
223+
"collapsed": false
224+
},
225+
"outputs": [],
226+
"source": [
227+
"import numpy as np\nfrom itertools import product\n\n\ndef squiggle_xy(a, b, c, d, i=np.arange(0.0, 2*np.pi, 0.05)):\n return np.sin(i*a)*np.cos(i*b), np.sin(i*c)*np.cos(i*d)\n\n\nfig11 = plt.figure(figsize=(8, 8), constrained_layout=False)\n\n# gridspec inside gridspec\nouter_grid = fig11.add_gridspec(4, 4, wspace=0.0, hspace=0.0)\n\nfor i in range(16):\n inner_grid = outer_grid[i].subgridspec(3, 3, wspace=0.0, hspace=0.0)\n a, b = int(i/4)+1, i % 4+1\n for j, (c, d) in enumerate(product(range(1, 4), repeat=2)):\n ax = fig11.add_subplot(inner_grid[j])\n ax.plot(*squiggle_xy(a, b, c, d))\n ax.set_xticks([])\n ax.set_yticks([])\n fig11.add_subplot(ax)\n\nall_axes = fig11.get_axes()\n\n# show only the outside spines\nfor ax in all_axes:\n for sp in ax.spines.values():\n sp.set_visible(False)\n if ax.is_first_row():\n ax.spines['top'].set_visible(True)\n if ax.is_last_row():\n ax.spines['bottom'].set_visible(True)\n if ax.is_first_col():\n ax.spines['left'].set_visible(True)\n if ax.is_last_col():\n ax.spines['right'].set_visible(True)\n\nplt.show()"
228+
]
229+
},
230+
{
231+
"cell_type": "markdown",
232+
"metadata": {},
233+
"source": [
234+
"------------\n\nReferences\n\"\"\"\"\"\"\"\"\"\"\n\nThe usage of the following functions and methods is shown in this example:\n\n"
235+
]
236+
},
237+
{
238+
"cell_type": "code",
239+
"execution_count": null,
240+
"metadata": {
241+
"collapsed": false
242+
},
243+
"outputs": [],
244+
"source": [
245+
"matplotlib.pyplot.subplots\nmatplotlib.figure.Figure.add_gridspec\nmatplotlib.figure.Figure.add_subplot\nmatplotlib.gridspec.GridSpec\nmatplotlib.gridspec.SubplotSpec.subgridspec\nmatplotlib.gridspec.GridSpecFromSubplotSpec"
246+
]
247+
}
248+
],
249+
"metadata": {
250+
"kernelspec": {
251+
"display_name": "Python 3",
252+
"language": "python",
253+
"name": "python3"
254+
},
255+
"language_info": {
256+
"codemirror_mode": {
257+
"name": "ipython",
258+
"version": 3
259+
},
260+
"file_extension": ".py",
261+
"mimetype": "text/x-python",
262+
"name": "python",
263+
"nbconvert_exporter": "python",
264+
"pygments_lexer": "ipython3",
265+
"version": "3.7.3"
266+
}
267+
},
268+
"nbformat": 4,
269+
"nbformat_minor": 0
270+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""
2+
===========
3+
Multi Image
4+
===========
5+
6+
Make a set of images with a single colormap, norm, and colorbar.
7+
"""
8+
9+
from matplotlib import colors
10+
import matplotlib.pyplot as plt
11+
import numpy as np
12+
13+
np.random.seed(19680801)
14+
Nr = 3
15+
Nc = 2
16+
cmap = "cool"
17+
18+
fig, axs = plt.subplots(Nr, Nc)
19+
fig.suptitle('Multiple images')
20+
21+
images = []
22+
for i in range(Nr):
23+
for j in range(Nc):
24+
# Generate data with a range that varies from one plot to the next.
25+
data = ((1 + i + j) / 10) * np.random.rand(10, 20) * 1e-6
26+
images.append(axs[i, j].imshow(data, cmap=cmap))
27+
axs[i, j].label_outer()
28+
29+
# Find the min and max of all colors for use in setting the color scale.
30+
vmin = min(image.get_array().min() for image in images)
31+
vmax = max(image.get_array().max() for image in images)
32+
norm = colors.Normalize(vmin=vmin, vmax=vmax)
33+
for im in images:
34+
im.set_norm(norm)
35+
36+
fig.colorbar(images[0], ax=axs, orientation='horizontal', fraction=.1)
37+
38+
39+
# Make images respond to changes in the norm of other images (e.g. via the
40+
# "edit axis, curves and images parameters" GUI on Qt), but be careful not to
41+
# recurse infinitely!
42+
def update(changed_image):
43+
for im in images:
44+
if (changed_image.get_cmap() != im.get_cmap()
45+
or changed_image.get_clim() != im.get_clim()):
46+
im.set_cmap(changed_image.get_cmap())
47+
im.set_clim(changed_image.get_clim())
48+
49+
50+
for im in images:
51+
im.callbacksSM.connect('changed', update)
52+
53+
plt.show()
54+
55+
#############################################################################
56+
#
57+
# ------------
58+
#
59+
# References
60+
# """"""""""
61+
#
62+
# The use of the following functions, methods and classes is shown
63+
# in this example:
64+
65+
import matplotlib
66+
matplotlib.axes.Axes.imshow
67+
matplotlib.pyplot.imshow
68+
matplotlib.figure.Figure.colorbar
69+
matplotlib.pyplot.colorbar
70+
matplotlib.colors.Normalize
71+
matplotlib.cm.ScalarMappable.set_cmap
72+
matplotlib.cm.ScalarMappable.set_norm
73+
matplotlib.cm.ScalarMappable.set_clim
74+
matplotlib.cbook.CallbackRegistry.connect
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
======================
3+
Whats New 0.99 Mplot3d
4+
======================
5+
6+
Create a 3D surface plot.
7+
"""
8+
import numpy as np
9+
import matplotlib.pyplot as plt
10+
from matplotlib import cm
11+
from mpl_toolkits.mplot3d import Axes3D
12+
13+
X = np.arange(-5, 5, 0.25)
14+
Y = np.arange(-5, 5, 0.25)
15+
X, Y = np.meshgrid(X, Y)
16+
R = np.sqrt(X**2 + Y**2)
17+
Z = np.sin(R)
18+
19+
fig = plt.figure()
20+
ax = Axes3D(fig)
21+
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.viridis)
22+
23+
plt.show()
24+
25+
#############################################################################
26+
#
27+
# ------------
28+
#
29+
# References
30+
# """"""""""
31+
#
32+
# The use of the following functions, methods, classes and modules is shown
33+
# in this example:
34+
35+
import mpl_toolkits
36+
mpl_toolkits.mplot3d.Axes3D
37+
mpl_toolkits.mplot3d.Axes3D.plot_surface

0 commit comments

Comments
 (0)