|
|
|||||||||||||||||||||||||||||
|
matplotlib units_Examples 90_artist_tests. |
H.Kamifuji . |
|
Matplotlibプリミティブアーティストの各タイプのテストユニットのサポート。 軸は単位変換を処理し、アーティストは軸の親を指すポインタを保持します。 ユニットのデータで使用する場合は、軸インスタンスでアーティストを初期化する必要があります。そうしないと、単位をスカラーに変換する方法がわかりません。
"""
============
Artist tests
============
Test unit support with each of the Matplotlib primitive artist types.
The axis handles unit conversions and the artists keep a pointer to their axis
parent. You must initialize the artists with the axis instance if you want to
use them with unit data, or else they will not know how to convert the units
to scalars.
"""
import random
import matplotlib.lines as lines
import matplotlib.patches as patches
import matplotlib.text as text
import matplotlib.collections as collections
from basic_units import cm, inch
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.xaxis.set_units(cm)
ax.yaxis.set_units(cm)
if 0:
# test a line collection
# Not supported at present.
verts = []
for i in range(10):
# a random line segment in inches
verts.append(zip(*inch*10*np.random.rand(2, random.randint(2, 15))))
lc = collections.LineCollection(verts, axes=ax)
ax.add_collection(lc)
# test a plain-ol-line
line = lines.Line2D([0*cm, 1.5*cm], [0*cm, 2.5*cm],
lw=2, color='black', axes=ax)
ax.add_line(line)
if 0:
# test a patch
# Not supported at present.
rect = patches.Rectangle((1*cm, 1*cm), width=5*cm, height=2*cm,
alpha=0.2, axes=ax)
ax.add_patch(rect)
t = text.Text(3*cm, 2.5*cm, 'text label', ha='left', va='bottom', axes=ax)
ax.add_artist(t)
ax.set_xlim(-1*cm, 10*cm)
ax.set_ylim(-1*cm, 10*cm)
# ax.xaxis.set_units(inch)
ax.grid(True)
ax.set_title("Artists with units")
plt.show()
|
このサンプルを実行するときに、basic_units_org.py が必要です。取り敢えず、実行時のカレント・ディレクトリに置けば動作します。![]() Python 3.11.2 見直しました。上記のコードでは、下記のエラーが発生します。 artist_tests.txt matplotlib 内部のエラーのようです。matplotlib の改修(先祖帰りバグの改修)を待つしかない。 Python 3.11.6 (matplotlib 3.7.1) では、下記のようなエラーがあり、実行できない。
Traceback (most recent call last):
File "C:\Users\______\AppData\Local\Programs\Python\Python311\Lib
\site-packages\matplotlib\axis.py", line 1732, in convert_units
ret = self.converter.convert(x, self.units, self)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "M:\______\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 "M:\______\artist_tests.py", line 40, in
Python 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:\______\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:\______\artist_tests.py", line 40, in
Python 3.11.6 (matplotlib 3.7.1) 及び Python 3.12.0 (matplotlib 3.8.1) で、見直し中、新しいサンプル(
units-artist-tests-py
) を見つけ、下記のコードで、正常に実行できました。""" ============ Artist tests ============ Test unit support with each of the Matplotlib primitive artist types. The axis handles unit conversions and the artists keep a pointer to their axis parent. You must initialize the artists with the axis instance if you want to use them with unit data, or else they will not know how to convert the units to scalars. .. only:: builder_html This example requires :download:`basic_units.pyPython 3.11.6 (matplotlib 3.7.1) 及び Python 3.12.0 (matplotlib 3.8.1) 共に、正常実行です。 このサンプルを実行するときに、basic_units.py が必要です。取り敢えず、実行時のカレント・ディレクトリに置けば動作します。 ![]() basic_units.py を新しいのに、入れ替えると、元の artist_tests.py でも、正常に実行できます。 ![]() |
|
units_Examples code: artist_tests.py units-artist-tests-py |
|