|
1 | 1 | """
|
2 | 2 | ==========
|
3 |
| -pcolormesh |
| 3 | +Pcolormesh |
4 | 4 | ==========
|
5 | 5 |
|
6 |
| -Shows how to combine Normalization and Colormap instances to draw "levels" in |
7 |
| -`~.axes.Axes.pcolor`, `~.axes.Axes.pcolormesh` and `~.axes.Axes.imshow` type |
8 |
| -plots in a similar way to the levels keyword argument to contour/contourf. |
| 6 | +`.axes.Axes.pcolormesh` allows you to generate 2-D image-style plots. Note it |
| 7 | +is somewhat faster than the similar `~.axes.Axes.pcolor`. |
| 8 | +
|
9 | 9 | """
|
10 | 10 |
|
11 | 11 | import matplotlib
|
|
14 | 14 | from matplotlib.ticker import MaxNLocator
|
15 | 15 | import numpy as np
|
16 | 16 |
|
| 17 | +############################################################################### |
| 18 | +# Basic Pcolormesh |
| 19 | +# ---------------- |
| 20 | +# |
| 21 | +# We usually specify a pcolormesh by defining the edge of quadrilaterals and |
| 22 | +# the value of the quadrilateral. Note that here *x* and *y* each have one |
| 23 | +# extra element than Z in the respective dimension. |
| 24 | + |
| 25 | +np.random.seed(19680801) |
| 26 | +Z = np.random.rand(6, 10) |
| 27 | +x = np.arange(-0.5, 10, 1) # len = 11 |
| 28 | +y = np.arange(4.5, 11, 1) # len = 7 |
| 29 | + |
| 30 | +fig, ax = plt.subplots() |
| 31 | +ax.pcolormesh(x, y, Z) |
| 32 | + |
| 33 | +############################################################################### |
| 34 | +# Non-rectilinear Pcolormesh |
| 35 | +# -------------------------- |
| 36 | +# |
| 37 | +# Note that we can also specify matrices for *X* and *Y* and have |
| 38 | +# non-rectilinear quadrilaterals. |
| 39 | + |
| 40 | +x = np.arange(-0.5, 10, 1) # len = 11 |
| 41 | +y = np.arange(4.5, 11, 1) # len = 7 |
| 42 | +X, Y = np.meshgrid(x, y) |
| 43 | +X = X + 0.2 * Y # tilt the co-ordinates. |
| 44 | +Y = Y + 0.3 * X |
| 45 | + |
| 46 | +fig, ax = plt.subplots() |
| 47 | +ax.pcolormesh(X, Y, Z) |
| 48 | + |
| 49 | +############################################################################### |
| 50 | +# Centered Co-ordinates |
| 51 | +# --------------------- |
| 52 | +# |
| 53 | +# Often a user wants to pass *X* and *Y* with the same sizes as *Z* to |
| 54 | +# `.axes.Axes.pcolormesh`. This is also allowed if ``shading='auto'`` is |
| 55 | +# passed (default set by :rc:`pcolor.shading`). Pre Matplotlib 3.3, |
| 56 | +# ``shading='flat'`` would drop the last column and row of *Z*; while that |
| 57 | +# is still allowed for back compatibility purposes, a Deprecation Warning is |
| 58 | +# raised. |
| 59 | + |
| 60 | +x = np.arange(10) # len = 10 |
| 61 | +y = np.arange(6) # len = 6 |
| 62 | +X, Y = np.meshgrid(x, y) |
| 63 | + |
| 64 | +fig, axs = plt.subplots(2, 1, sharex=True, sharey=True) |
| 65 | +axs[0].pcolormesh(X, Y, Z, vmin=np.min(Z), vmax=np.max(Z), shading='auto') |
| 66 | +axs[0].set_title("shading='auto' = 'nearest'") |
| 67 | +axs[1].pcolormesh(X, Y, Z, vmin=np.min(Z), vmax=np.max(Z), shading='flat') |
| 68 | +axs[1].set_title("shading='flat'") |
| 69 | + |
| 70 | +############################################################################### |
| 71 | +# Making levels using Norms |
| 72 | +# ------------------------- |
| 73 | +# |
| 74 | +# Shows how to combine Normalization and Colormap instances to draw |
| 75 | +# "levels" in `.axes.Axes.pcolor`, `.axes.Axes.pcolormesh` |
| 76 | +# and `.axes.Axes.imshow` type plots in a similar |
| 77 | +# way to the levels keyword argument to contour/contourf. |
17 | 78 |
|
18 | 79 | # make these smaller to increase the resolution
|
19 | 80 | dx, dy = 0.05, 0.05
|
|
54 | 115 | # don't overlap
|
55 | 116 | fig.tight_layout()
|
56 | 117 |
|
57 |
| -plt.show() |
58 |
| - |
59 | 118 | #############################################################################
|
60 | 119 | #
|
61 | 120 | # ------------
|
|
0 commit comments