|
|
|||||||||||||||||||||||||||||
|
images_contours_and_fields_Examples 01_contourf_log. |
H.Kamifuji . |
|
contourf でのログカラースケールの使用のデモンストレーション この事例は、Windows10_1909 で Python 3.9.0 環境では、動作しません。( module 'matplotlib.mlab' has no attribute 'bivariate_normal'が 3.1 から削除されたのか? )
'''
Demonstrate use of a log color scale in contourf
'''
import matplotlib.pyplot as plt
import numpy as np
from numpy import ma
from matplotlib import colors, ticker, cm
from matplotlib.mlab import bivariate_normal
N = 100
x = np.linspace(-3.0, 3.0, N)
y = np.linspace(-2.0, 2.0, N)
X, Y = np.meshgrid(x, y)
# A low hump with a spike coming out of the top right.
# Needs to have z/colour axis on a log scale so we see both hump and spike.
# linear scale only shows the spike.
z = (bivariate_normal(X, Y, 0.1, 0.2, 1.0, 1.0)
+ 0.1 * bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0))
# Put in some negative values (lower left corner) to cause trouble with logs:
z[:5, :5] = -1
# The following is not strictly essential, but it will eliminate
# a warning. Comment it out to see the warning.
z = ma.masked_where(z <= 0, z)
# Automatic selection of levels works; setting the
# log locator tells contourf to use a log scale:
fig, ax = plt.subplots()
cs = ax.contourf(X, Y, z, locator=ticker.LogLocator(), cmap=cm.PuBu_r)
# Alternatively, you can manually set the levels
# and the norm:
#lev_exp = np.arange(np.floor(np.log10(z.min())-1),
# np.ceil(np.log10(z.max())+1))
#levs = np.power(10, lev_exp)
#cs = P.contourf(X, Y, z, levs, norm=colors.LogNorm())
# The 'extend' kwarg does not work yet with a log scale.
cbar = fig.colorbar(cs)
plt.show()
|
![]() Python 3.11.2 見直しました。上記のコードでは、下記のエラーが発生します。 Traceback (most recent call last): File "_:\contourf_log.py", line 9, in <module> from matplotlib.mlab import bivariate_normal ImportError: cannot import name 'bivariate_normal' from 'matplotlib.mlab' (C:\Users\_____\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib\mlab.py) matplotlib 内部のエラーのようです。matplotlib の改修(先祖帰りバグの改修)を待つしかない。 Python 3.11.6 (matplotlib 3.7.1) では、下記のようなエラーがあり、実行できない。 Traceback (most recent call last): File "M:\______\contourf_log.py", line 9, inPython 3.12.0 (matplotlib 3.8.1) では、下記のようなエラーがあり、実行できない。 Traceback (most recent call last): File "E:\______\contourf_log.py", line 9, inPython 3.11.6 (matplotlib 3.7.1) 及び Python 3.12.0 (matplotlib 3.8.1) で、見直し中、新しいサンプル(images-contours-and-fields-contourf-log-py) を見つけ、下記のコードで、正常に実行できました。 """ ============================ Contourf and log color scale ============================ Demonstrate use of a log color scale in contourf """ import matplotlib.pyplot as plt import numpy as np from numpy import ma from matplotlib import cm, ticker N = 100 x = np.linspace(-3.0, 3.0, N) y = np.linspace(-2.0, 2.0, N) X, Y = np.meshgrid(x, y) # A low hump with a spike coming out. # Needs to have z/colour axis on a log scale, so we see both hump and spike. # A linear scale only shows the spike. Z1 = np.exp(-X**2 - Y**2) Z2 = np.exp(-(X * 10)**2 - (Y * 10)**2) z = Z1 + 50 * Z2 # Put in some negative values (lower left corner) to cause trouble with logs: z[:5, :5] = -1 # The following is not strictly essential, but it will eliminate # a warning. Comment it out to see the warning. z = ma.masked_where(z <= 0, z) # Automatic selection of levels works; setting the # log locator tells contourf to use a log scale: fig, ax = plt.subplots() cs = ax.contourf(X, Y, z, locator=ticker.LogLocator(), cmap=cm.PuBu_r) # Alternatively, you can manually set the levels # and the norm: # lev_exp = np.arange(np.floor(np.log10(z.min())-1), # np.ceil(np.log10(z.max())+1)) # levs = np.power(10, lev_exp) # cs = ax.contourf(X, Y, z, levs, norm=colors.LogNorm()) cbar = fig.colorbar(cs) plt.show() # %% # # .. admonition:: References # # The use of the following functions, methods, classes and modules is shown # in this example: # # - `matplotlib.axes.Axes.contourf` / `matplotlib.pyplot.contourf` # - `matplotlib.figure.Figure.colorbar` / `matplotlib.pyplot.colorbar` # - `matplotlib.axes.Axes.legend` / `matplotlib.pyplot.legend` # - `matplotlib.ticker.LogLocator`Python 3.11.6 (matplotlib 3.7.1) 及び Python 3.12.0 (matplotlib 3.8.1) 共に、正常実行です。 ![]() |
|
images_contours_and_fields_Examples code: contourf_log.py images-contours-and-fields-contourf-log-py |
|