|
|
matplotlib units_Examples 94_ellipse_with_units. |
H.Kamifuji . |
多角形近似に対する円弧で生成された楕円を比較する""" Compare the ellipse generated with arcs versus a polygonal approximation """ from basic_units import cm import numpy as np from matplotlib import patches import matplotlib.pyplot as plt xcenter, ycenter = 0.38*cm, 0.52*cm #xcenter, ycenter = 0., 0. width, height = 1e-1*cm, 3e-1*cm angle = -30 theta = np.arange(0.0, 360.0, 1.0)*np.pi/180.0 x = 0.5 * width * np.cos(theta) y = 0.5 * height * np.sin(theta) rtheta = np.radians(angle) R = np.array([ [np.cos(rtheta), -np.sin(rtheta)], [np.sin(rtheta), np.cos(rtheta)], ]) x, y = np.dot(R, np.array([x, y])) x += xcenter y += ycenter fig = plt.figure() ax = fig.add_subplot(211, aspect='auto') ax.fill(x, y, alpha=0.2, facecolor='yellow', edgecolor='yellow', linewidth=1, zorder=1) e1 = patches.Ellipse((xcenter, ycenter), width, height, angle=angle, linewidth=2, fill=False, zorder=2) ax.add_patch(e1) ax = fig.add_subplot(212, aspect='equal') ax.fill(x, y, alpha=0.2, facecolor='green', edgecolor='green', zorder=1) e2 = patches.Ellipse((xcenter, ycenter), width, height, angle=angle, linewidth=2, fill=False, zorder=2) ax.add_patch(e2) #fig.savefig('ellipse_compare.png') fig.savefig('ellipse_compare') fig = plt.figure() ax = fig.add_subplot(211, aspect='auto') ax.fill(x, y, alpha=0.2, facecolor='yellow', edgecolor='yellow', linewidth=1, zorder=1) e1 = patches.Arc((xcenter, ycenter), width, height, angle=angle, linewidth=2, fill=False, zorder=2) ax.add_patch(e1) ax = fig.add_subplot(212, aspect='equal') ax.fill(x, y, alpha=0.2, facecolor='green', edgecolor='green', zorder=1) e2 = patches.Arc((xcenter, ycenter), width, height, angle=angle, linewidth=2, fill=False, zorder=2) ax.add_patch(e2) #fig.savefig('arc_compare.png') fig.savefig('arc_compare') plt.show() |
このサンプルを実行するときに、basic_units_org.py が必要です。取り敢えず、実行時のカレント・ディレクトリに置けば動作します。![]() ![]() Python 3.11.2 見直しました。上記のコードでは、下記のエラーが発生します。 ellipse_with_units.txt matplotlib 内部のエラーのようです。matplotlib の改修(先祖帰りバグの改修)を待つしかない。 Python 3.11.6 (matplotlib 3.7.1) では、下記のようなエラーがあり、実行できない。 M:\______\ellipse_with_units.py:26: FutureWarning: The input object of type 'TaggedValue' is an array-like implementing one of the corresponding protocols (`__array__`, `__array_interface__` or `__array_struct__`); but not a sequence (or 0-D). In the future, this object will be coerced as if it was first converted using `np.array(obj)`. To retain the old behaviour, you have to either modify the type 'TaggedValue', or assign to an empty array created with `np.empty(correct_shape, dtype=object)`. x, y = np.dot(R, np.array([x, y])) Traceback (most recent call last): File "M:\______\ellipse_with_units.py", line 26, inPython 3.12.0 (matplotlib 3.8.1) では、下記のようなエラーがあり、実行できない。 Traceback (most recent call last): File "C:\Program Files\Python312\Lib\site-packages\matplotlib\axis.py", line 1769, in convert_units ret = self.converter.convert(x, self.units, self) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "E:\E_Home\usr\Python\Py-Support\76_units_Examples\94_ellipse_with_units\basic_units.py", line 341, in convert if units.ConversionInterface.is_numlike(val): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: type object 'ConversionInterface' has no attribute 'is_numlike' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "E:\______\ellipse_with_units.py", line 37, inPython 3.11.6 (matplotlib 3.7.1) 及び Python 3.12.0 (matplotlib 3.8.1) で、見直し中、新しいサンプル(units-ellipse-with-units-py) を見つけ、下記のコードで、正常に実行できました。 """ ================== Ellipse with units ================== Compare the ellipse generated with arcs versus a polygonal approximation. .. only:: builder_html This example requires :download:`basic_units.py basic_units.py` """ from basic_units import cm import matplotlib.pyplot as plt import numpy as np from matplotlib import patches xcenter, ycenter = 0.38*cm, 0.52*cm width, height = 1e-1*cm, 3e-1*cm angle = -30 theta = np.deg2rad(np.arange(0.0, 360.0, 1.0)) x = 0.5 * width * np.cos(theta) y = 0.5 * height * np.sin(theta) rtheta = np.radians(angle) R = np.array([ [np.cos(rtheta), -np.sin(rtheta)], [np.sin(rtheta), np.cos(rtheta)], ]) x, y = np.dot(R, [x, y]) x += xcenter y += ycenter # %% fig = plt.figure() ax = fig.add_subplot(211, aspect='auto') ax.fill(x, y, alpha=0.2, facecolor='yellow', edgecolor='yellow', linewidth=1, zorder=1) e1 = patches.Ellipse((xcenter, ycenter), width, height, angle=angle, linewidth=2, fill=False, zorder=2) ax.add_patch(e1) ax = fig.add_subplot(212, aspect='equal') ax.fill(x, y, alpha=0.2, facecolor='green', edgecolor='green', zorder=1) e2 = patches.Ellipse((xcenter, ycenter), width, height, angle=angle, linewidth=2, fill=False, zorder=2) ax.add_patch(e2) fig.savefig('ellipse_compare') # %% fig = plt.figure() ax = fig.add_subplot(211, aspect='auto') ax.fill(x, y, alpha=0.2, facecolor='yellow', edgecolor='yellow', linewidth=1, zorder=1) e1 = patches.Arc((xcenter, ycenter), width, height, angle=angle, linewidth=2, fill=False, zorder=2) ax.add_patch(e1) ax = fig.add_subplot(212, aspect='equal') ax.fill(x, y, alpha=0.2, facecolor='green', edgecolor='green', zorder=1) e2 = patches.Arc((xcenter, ycenter), width, height, angle=angle, linewidth=2, fill=False, zorder=2) ax.add_patch(e2) fig.savefig('arc_compare') plt.show()Python 3.11.6 (matplotlib 3.7.1) 及び Python 3.12.0 (matplotlib 3.8.1) 共に、正常実行です。 このサンプルを実行するときに、basic_units.py が必要です。取り敢えず、実行時のカレント・ディレクトリに置けば動作します。 ![]() ![]() basic_units.py を新しいのに、入れ替えると、元の ellipse_with_units.py でも、正常に実行できます。 ![]() ![]() |
units_Examples code: ellipse_with_units.py units-ellipse-with-units-py |
|