|

Python 3.11.6 (matplotlib 3.7.1) では、下記のような警告があるが、実行できる。matplotlib 3.8.x 以降に使えないようです。
M:\______\patch_collection.py:44: MatplotlibDeprecationWarning:
Passing the closed parameter of __init__() positionally is deprecated
since Matplotlib 3.6; the parameter will become keyword-only two minor
releases later.
polygon = Polygon(np.random.rand(N, 2), True)
Python 3.12.0 (matplotlib 3.8.1) では、予告通り、下記のようなエラーがあり、実行できない。
Traceback (most recent call last):
File "E:\______\patch_collection.py", line 44, in
polygon = Polygon(np.random.rand(N, 2), True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Polygon.__init__() takes 2 positional arguments but 3 were given
Python 3.11.6 (matplotlib 3.7.1) 及び Python 3.12.0 (matplotlib 3.8.1) で、見直し中、新しいサンプル(patch-collection-py") を見つけ、下記のコードで、正常に実行できました。
"""
============================
Circles, Wedges and Polygons
============================
This example demonstrates how to use `.collections.PatchCollection`.
See also :doc:`/gallery/shapes_and_collections/artist_reference`, which instead
adds each artist separately to its own axes.
"""
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.collections import PatchCollection
from matplotlib.patches import Circle, Polygon, Wedge
# Fixing random state for reproducibility
np.random.seed(19680801)
fig, ax = plt.subplots()
resolution = 50 # the number of vertices
N = 3
x = np.random.rand(N)
y = np.random.rand(N)
radii = 0.1*np.random.rand(N)
patches = []
for x1, y1, r in zip(x, y, radii):
circle = Circle((x1, y1), r)
patches.append(circle)
x = np.random.rand(N)
y = np.random.rand(N)
radii = 0.1*np.random.rand(N)
theta1 = 360.0*np.random.rand(N)
theta2 = 360.0*np.random.rand(N)
for x1, y1, r, t1, t2 in zip(x, y, radii, theta1, theta2):
wedge = Wedge((x1, y1), r, t1, t2)
patches.append(wedge)
# Some limiting conditions on Wedge
patches += [
Wedge((.3, .7), .1, 0, 360), # Full circle
Wedge((.7, .8), .2, 0, 360, width=0.05), # Full ring
Wedge((.8, .3), .2, 0, 45), # Full sector
Wedge((.8, .3), .2, 45, 90, width=0.10), # Ring sector
]
for i in range(N):
polygon = Polygon(np.random.rand(N, 2), closed=True)
patches.append(polygon)
colors = 100 * np.random.rand(len(patches))
p = PatchCollection(patches, alpha=0.4)
p.set_array(colors)
ax.add_collection(p)
fig.colorbar(p, ax=ax)
plt.show()
# %%
#
# .. admonition:: References
#
# The use of the following functions, methods, classes and modules is shown
# in this example:
#
# - `matplotlib.patches`
# - `matplotlib.patches.Circle`
# - `matplotlib.patches.Wedge`
# - `matplotlib.patches.Polygon`
# - `matplotlib.collections.PatchCollection`
# - `matplotlib.collections.Collection.set_array`
# - `matplotlib.axes.Axes.add_collection`
# - `matplotlib.figure.Figure.colorbar`

|