|
|
|||||||||||||||||||||||||||||
|
pylab_examples_Examples 37_contour_corner_mask. |
H.Kamifuji . |
マスクされた等高線図の場合、corner_mask = Falseとcorner_mask = True の違いを示します。
"""
Illustrate the difference between corner_mask=False and corner_mask=True
for masked contour plots.
"""
import matplotlib.pyplot as plt
import numpy as np
# Data to plot.
x, y = np.meshgrid(np.arange(7), np.arange(10))
z = np.sin(0.5*x)*np.cos(0.52*y)
# Mask various z values.
mask = np.zeros_like(z, dtype=np.bool)
mask[2, 3:5] = True
mask[3:5, 4] = True
mask[7, 2] = True
mask[5, 0] = True
mask[0, 6] = True
z = np.ma.array(z, mask=mask)
corner_masks = [False, True]
for i, corner_mask in enumerate(corner_masks):
plt.subplot(1, 2, i+1)
cs = plt.contourf(x, y, z, corner_mask=corner_mask)
plt.contour(cs, colors='k')
plt.title('corner_mask = {0}'.format(corner_mask))
# Plot grid.
plt.grid(c='k', ls='-', alpha=0.3)
# Indicate masked points with red circles.
plt.plot(np.ma.array(x, mask=~mask), y, 'ro')
plt.show()
|
![]() Python 3.11.2 見直しました。上記のコードでは、下記のエラーが発生します。 _:\contour_corner_mask.py:13: FutureWarning: In the future `np.bool` will be defined as the corresponding NumPy scalar. mask = np.zeros_like(z, dtype=np.bool) Traceback (most recent call last): File "_:\contour_corner_mask.py", line 13, in <module> mask = np.zeros_like(z, dtype=np.bool) ^^^^^^^ File "C:\Users\_____\AppData\Local\Programs\Python\Python311\Lib\site-packages\numpy\__init__.py", line 305, in __getattr__ raise AttributeError(__former_attrs__[attr]) AttributeError: module 'numpy' has no attribute 'bool'. `np.bool` was a deprecated alias for the builtin `bool`. To avoid this error in existing code, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here. The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations. Did you mean: 'bool_'? matplotlib 内部のエラーのようです。matplotlib の改修(先祖帰りバグの改修)を待つしかない。 Python 3.11.6 (matplotlib 3.7.1) では、下記のようなエラーがあり、実行できない。
M:\______\contour_corner_mask.py:13: FutureWarning:
In the future `np.bool` will be defined as the corresponding NumPy scalar.
mask = np.zeros_like(z, dtype=np.bool)
Traceback (most recent call last):
File "M:\______\contour_corner_mask.py", line 13, in
Python 3.12.0 (matplotlib 3.8.1) では、下記のようなエラーがあり、実行できない。
E:\______\contour_corner_mask.py:13: FutureWarning:
In the future `np.bool` will be defined as the corresponding NumPy scalar.
mask = np.zeros_like(z, dtype=np.bool)
Traceback (most recent call last):
File "E:\______\contour_corner_mask.py", line 13, in
Python 3.11.6 (matplotlib 3.7.1) 及び Python 3.12.0 (matplotlib 3.8.1) で、見直し中、新しいサンプル(images-contours-and-fields-contour-corner-mask-py) を見つけ、下記のコードで、正常に実行できました。
"""
===================
Contour Corner Mask
===================
Illustrate the difference between ``corner_mask=False`` and
``corner_mask=True`` for masked contour plots. The default is controlled by
:rc:`contour.corner_mask`.
"""
import matplotlib.pyplot as plt
import numpy as np
# Data to plot.
x, y = np.meshgrid(np.arange(7), np.arange(10))
z = np.sin(0.5 * x) * np.cos(0.52 * y)
# Mask various z values.
mask = np.zeros_like(z, dtype=bool)
mask[2, 3:5] = True
mask[3:5, 4] = True
mask[7, 2] = True
mask[5, 0] = True
mask[0, 6] = True
z = np.ma.array(z, mask=mask)
corner_masks = [False, True]
fig, axs = plt.subplots(ncols=2)
for ax, corner_mask in zip(axs, corner_masks):
cs = ax.contourf(x, y, z, corner_mask=corner_mask)
ax.contour(cs, colors='k')
ax.set_title(f'{corner_mask=}')
# Plot grid.
ax.grid(c='k', ls='-', alpha=0.3)
# Indicate masked points with red circles.
ax.plot(np.ma.array(x, mask=~mask), y, 'ro')
plt.show()
# %%
#
# .. admonition:: References
#
# The use of the following functions, methods, classes and modules is shown
# in this example:
#
# - `matplotlib.axes.Axes.contour` / `matplotlib.pyplot.contour`
# - `matplotlib.axes.Axes.contourf` / `matplotlib.pyplot.contourf`
Python 3.11.6 (matplotlib 3.7.1) 及び Python 3.12.0 (matplotlib 3.8.1) 共に、正常実行です。![]() |
|
pylab_examples_Examples code: contour_corner_mask.py images-contours-and-fields-contour-corner-mask-py |
|