Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d449a4e

Browse files
committed
FIX: add shading='nearest' and warn on mis-sized shading='flat'
1 parent 15e8d38 commit d449a4e

File tree

13 files changed

+452
-100
lines changed

13 files changed

+452
-100
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Pcolor and Pcolormesh now accept shading='nearest' and 'auto'
2+
-------------------------------------------------------------
3+
4+
Previously `.axes.Axes.pcolor` and `.axes.Axes.pcolormesh` handled
5+
the situation where *x* and *y* have the same (respective) size as *C* by
6+
dropping the last row and column of *C*, and *x* and *y* are regarded as the
7+
edges of the remaining rows and columns in *C*. However, many users probably
8+
really want *x* and *y* centered on the rows and columns of *C*.
9+
10+
To accommodate this, ``shading='nearest'`` and ``shading='auto'`` are
11+
new allowed strings for the ``shading`` kwarg. ``'nearest'`` will center the
12+
color on *x* and *y* if *x* and *y* have the same dimensions as *C*
13+
(otherwise an error will be thrown). ``shading='auto'`` will choose 'flat'
14+
or 'nearest' based on the size of *X*, *Y*, *C*.
15+
16+
If ``shading='flat'`` then *X*, and *Y* should have dimensions one larger
17+
than *C*. If *X* and *Y* have the same dimensions as *C*, then the previous
18+
behavior is used and the last row and column of *C* are dropped, and a
19+
Deprecation Warning is raised.
20+
21+
Users can also specify this by the new :rc:`pcolor.shading` in their
22+
``.matplotlibrc`` or via `.rcParams`.
23+
24+
See :doc:`pcolormesh </gallery/images_contours_and_fields/pcolormesh_grids>`
25+
for examples.
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
"""
2+
============================
3+
Pcolormesh grids and shading
4+
============================
5+
6+
`.axes.Axes.pcolormesh` and `~.axes.Axes.pcolor` have a few options for
7+
how grids are laid out and the shading between the grid points.
8+
9+
Generally, if *Z* is size *MxN* then the grid *X* and *Y* can be
10+
specified with either size *M+1 x N+1* or *MxN*, depending on the
11+
argument for the ``shading`` keyword argument. Note that below we specify
12+
vectors *x* as either length N or N+1 and *y* as length M or M+1, and
13+
`~.axes.Axes.pcolormesh` internally makes the mesh matrices *X* and *Y* from
14+
the input vectors.
15+
16+
"""
17+
18+
import matplotlib
19+
import matplotlib.pyplot as plt
20+
import numpy as np
21+
22+
###############################################################################
23+
# Flat Shading
24+
# ------------
25+
#
26+
# The grid specification with the least assumptions is ``shading='flat'``
27+
# and if the grid is one larger than the data in each dimesion, i.e. has size
28+
# *M+1 x N+1*. In that case *X* and *Y* sepcify the corners of quadrilaterals
29+
# that are colored with the values in *Z*. Here we specify the edges of the
30+
# *3x5* quadirlaterals with *X* and *Y* that are *4x6*.
31+
32+
np.random.seed(19680801)
33+
Z = np.arange(15).reshape(3, 5)
34+
x = np.arange(6) # len = 6
35+
y = np.arange(4) # len = 4
36+
37+
fig, ax = plt.subplots()
38+
ax.pcolormesh(x, y, Z, shading='flat', vmin=Z.min(), vmax=Z.max())
39+
40+
41+
def _annote(ax, x, y, title):
42+
# this all gets repeated below:
43+
X, Y = np.meshgrid(x, y)
44+
ax.plot(X.flat, Y.flat, 'o', color='m')
45+
ax.set_xlim(-0.7, 5.2)
46+
ax.set_ylim(-0.7, 3.2)
47+
ax.set_title(title)
48+
49+
_annote(ax, x, y, "shading='flat'")
50+
51+
52+
###############################################################################
53+
# Flat Shading, same size grid
54+
# ----------------------------
55+
#
56+
# Often, however, data is provided where *X* and *Y* match the size of *Z*.
57+
# As of Matplotlib v3.3, ``shading='flat'`` is Deprecated when this is the
58+
# case, a warning is raised, and the last row and column of *Z* are dropped.
59+
# This dropping of the last row and column is what Matplotlib did silently
60+
# previous to v3.3, and is compatible with what Matlab does.
61+
62+
x = np.arange(5) # len = 5
63+
y = np.arange(3) # len = 3
64+
65+
fig, ax = plt.subplots()
66+
ax.pcolormesh(x, y, Z, shading='flat', vmin=Z.min(), vmax=Z.max())
67+
_annote(ax, x, y, "shading='flat': X, Y, C same size")
68+
69+
###############################################################################
70+
# Nearest Shading, same size grid
71+
# -------------------------------
72+
#
73+
# Usually, dropping a row and column of data is not what the user means when
74+
# the make *X*, *Y* and *Z* all the same size. For this case, Matplotlib
75+
# allows ``shading='nearest'`` and centers the colored qudrilaterals on the
76+
# grid points.
77+
#
78+
# If a grid that is not the correct size is passed with ``shading='nearest'``
79+
# an Error is raised.
80+
81+
fig, ax = plt.subplots()
82+
ax.pcolormesh(x, y, Z, shading='nearest', vmin=Z.min(), vmax=Z.max())
83+
_annote(ax, x, y, "shading='nearest'")
84+
85+
###############################################################################
86+
# Auto Shading
87+
# ------------
88+
#
89+
# Its possible that the user would like the code to automatically choose
90+
# which to use, in which case ``shading='auto'`` will decide whether to
91+
# use 'flat' or 'nearest' shading based on the sizes of *X*, *Y* and *Z*.
92+
93+
fig, axs = plt.subplots(2, 1, constrained_layout=True)
94+
ax = axs[0]
95+
x = np.arange(5) # len = 5
96+
y = np.arange(3) # len = 3
97+
ax.pcolormesh(x, y, Z, shading='auto', vmin=Z.min(), vmax=Z.max())
98+
_annote(ax, x, y, "shading='auto'; X, Y, Z: same size (nearest)")
99+
100+
ax = axs[1]
101+
x = np.arange(6) # len = 5
102+
y = np.arange(4) # len = 3
103+
ax.pcolormesh(x, y, Z, shading='auto', vmin=Z.min(), vmax=Z.max())
104+
_annote(ax, x, y, "shading='auto'; X, Y one larger than Z (flat)")
105+
106+
###############################################################################
107+
# Gouraud Shading
108+
# ---------------
109+
#
110+
# `Gouraud shading <https://en.wikipedia.org/wiki/Gouraud_shading>`_ can also
111+
# be specified, where the colour in the quadrilaterals is linearly
112+
# interpolated between the grid points. The sizes of *X*, *Y*, *Z* must
113+
# be the same.
114+
115+
fig, ax = plt.subplots(constrained_layout=True)
116+
x = np.arange(5) # len = 5
117+
y = np.arange(3) # len = 3
118+
ax.pcolormesh(x, y, Z, shading='gouraud', vmin=Z.min(), vmax=Z.max())
119+
_annote(ax, x, y, "shading='gouruad'; X, Y same size as Z")
120+
121+
plt.show()
122+
#############################################################################
123+
#
124+
# ------------
125+
#
126+
# References
127+
# """"""""""
128+
#
129+
# The use of the following functions and methods is shown in this example:
130+
131+
matplotlib.axes.Axes.pcolormesh
132+
matplotlib.pyplot.pcolormesh

examples/images_contours_and_fields/pcolormesh_levels.py

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""
22
==========
3-
pcolormesh
3+
Pcolormesh
44
==========
55
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+
99
"""
1010

1111
import matplotlib
@@ -14,6 +14,67 @@
1414
from matplotlib.ticker import MaxNLocator
1515
import numpy as np
1616

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 coordinates.
44+
Y = Y + 0.3 * X
45+
46+
fig, ax = plt.subplots()
47+
ax.pcolormesh(X, Y, Z)
48+
49+
###############################################################################
50+
# Centered Coordinates
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.
1778

1879
# make these smaller to increase the resolution
1980
dx, dy = 0.05, 0.05
@@ -54,8 +115,6 @@
54115
# don't overlap
55116
fig.tight_layout()
56117

57-
plt.show()
58-
59118
#############################################################################
60119
#
61120
# ------------

0 commit comments

Comments
 (0)