Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit581dd72

Browse files
committed
FIX: add shading='nearest' and warn on mis-sized shading='flat'
1 parent63cbb60 commit581dd72

File tree

13 files changed

+460
-104
lines changed

13 files changed

+460
-104
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 want
8+
*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+
DeprecationWarning is emitted.
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* has shape *(M, N)* then the grid *X* and *Y* can be
10+
specified with either shape *(M+1, N+1)* or *(M, N)*, 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+
importmatplotlib
19+
importmatplotlib.pyplotasplt
20+
importnumpyasnp
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 shape
28+
# *(M+1, 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+
# *(3, 5)* quadrilaterals with *X* and *Y* that are *(4, 6)*.
31+
32+
nrows=3
33+
ncols=5
34+
Z=np.arange(nrows*ncols).reshape(nrows,ncols)
35+
x=np.arange(ncols+1)
36+
y=np.arange(nrows+1)
37+
38+
fig,ax=plt.subplots()
39+
ax.pcolormesh(x,y,Z,shading='flat',vmin=Z.min(),vmax=Z.max())
40+
41+
42+
def_annotate(ax,x,y,title):
43+
# this all gets repeated below:
44+
X,Y=np.meshgrid(x,y)
45+
ax.plot(X.flat,Y.flat,'o',color='m')
46+
ax.set_xlim(-0.7,5.2)
47+
ax.set_ylim(-0.7,3.2)
48+
ax.set_title(title)
49+
50+
_annotate(ax,x,y,"shading='flat'")
51+
52+
53+
###############################################################################
54+
# Flat Shading, same shape grid
55+
# -----------------------------
56+
#
57+
# Often, however, data is provided where *X* and *Y* match the shape of *Z*.
58+
# As of Matplotlib v3.3, ``shading='flat'`` is deprecated when this is the
59+
# case, a warning is raised, and the last row and column of *Z* are dropped.
60+
# This dropping of the last row and column is what Matplotlib did silently
61+
# previous to v3.3, and is compatible with what Matlab does.
62+
63+
x=np.arange(ncols)# note *not* ncols + 1 as before
64+
y=np.arange(nrows)
65+
fig,ax=plt.subplots()
66+
ax.pcolormesh(x,y,Z,shading='flat',vmin=Z.min(),vmax=Z.max())
67+
_annotate(ax,x,y,"shading='flat': X, Y, C same shape")
68+
69+
###############################################################################
70+
# Nearest Shading, same shape 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 shape. 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 shape 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+
_annotate(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 shapes of *X*, *Y* and *Z*.
92+
93+
fig,axs=plt.subplots(2,1,constrained_layout=True)
94+
ax=axs[0]
95+
x=np.arange(ncols)
96+
y=np.arange(nrows)
97+
ax.pcolormesh(x,y,Z,shading='auto',vmin=Z.min(),vmax=Z.max())
98+
_annotate(ax,x,y,"shading='auto'; X, Y, Z: same shape (nearest)")
99+
100+
ax=axs[1]
101+
x=np.arange(ncols+1)
102+
y=np.arange(nrows+1)
103+
ax.pcolormesh(x,y,Z,shading='auto',vmin=Z.min(),vmax=Z.max())
104+
_annotate(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 shapes of *X*, *Y*, *Z* must
113+
# be the same.
114+
115+
fig,ax=plt.subplots(constrained_layout=True)
116+
x=np.arange(ncols)
117+
y=np.arange(nrows)
118+
ax.pcolormesh(x,y,Z,shading='gouraud',vmin=Z.min(),vmax=Z.max())
119+
_annotate(ax,x,y,"shading='gouraud'; X, Y same shape 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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""
22
==========
3-
pcolormesh
3+
Pcolormesh
44
==========
55
6-
Shows howtocombine 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 youtogenerate 2-D image-style plots. Note it
7+
is faster than the similar`~.axes.Axes.pcolor`.
8+
99
"""
1010

1111
importmatplotlib
@@ -14,6 +14,67 @@
1414
frommatplotlib.tickerimportMaxNLocator
1515
importnumpyasnp
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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp