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

Skip to content

Commit 978f507

Browse files
committed
FIX: add shading='nearest' and warn on mis-sized shading='flat'
1 parent dff8650 commit 978f507

File tree

7 files changed

+298
-78
lines changed

7 files changed

+298
-78
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_levels>`
25+
for examples.

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 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.
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)