|
|
|||||||||||||||||||||||||||||
|
matplotlib mplot3d_Examples 14_pathpatch3d_demo. |
H.Kamifuji . |
pathpatch_2d_to_3d を使用して、3D プロット上に図形やテキストを描画する方法を示します。
"""
============================
Draw flat objects in 3D plot
============================
Demonstrate using pathpatch_2d_to_3d to 'draw' shapes and text on a 3D plot.
"""
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, PathPatch
# register Axes3D class with matplotlib by importing Axes3D
from mpl_toolkits.mplot3d import Axes3D
import mpl_toolkits.mplot3d.art3d as art3d
from matplotlib.text import TextPath
from matplotlib.transforms import Affine2D
def text3d(ax, xyz, s, zdir="z", size=None, angle=0, usetex=False, **kwargs):
x, y, z = xyz
if zdir == "y":
xy1, z1 = (x, z), y
elif zdir == "y":
xy1, z1 = (y, z), x
else:
xy1, z1 = (x, y), z
text_path = TextPath((0, 0), s, size=size, usetex=usetex)
trans = Affine2D().rotate(angle).translate(xy1[0], xy1[1])
p1 = PathPatch(trans.transform_path(text_path), **kwargs)
ax.add_patch(p1)
art3d.pathpatch_2d_to_3d(p1, z=z1, zdir=zdir)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
p = Circle((5, 5), 3)
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, z=0, zdir="x")
text3d(ax, (4, -2, 0), "X-axis", zdir="z", size=.5, usetex=False,
ec="none", fc="k")
text3d(ax, (12, 4, 0), "Y-axis", zdir="z", size=.5, usetex=False,
angle=.5*3.14159, ec="none", fc="k")
text3d(ax, (12, 10, 4), "Z-axis", zdir="y", size=.5, usetex=False,
angle=.5*3.14159, ec="none", fc="k")
text3d(ax, (1, 5, 0),
r"$\displaystyle G_{\mu\nu} + \Lambda g_{\mu\nu} = "
r"\frac{8\pi G}{c^4} T_{\mu\nu} $",
zdir="z", size=1, usetex=True,
ec="none", fc="k")
ax.set_xlim3d(0, 10)
ax.set_ylim3d(0, 10)
ax.set_zlim3d(0, 10)
plt.show()
|
|
当方の環境では、動作しない。 Python 3.11.2 見直しました。上記のコードでは、下記のエラーが発生します。 Traceback (most recent call last): File "C:\Users\_____\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib\texmanager.py", line 255, in _run_checked_subprocess report = subprocess.check_output( ^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\_____\AppData\Local\Programs\Python\Python311\Lib\subprocess.py", line 466, in check_output return run(*popenargs, stdout=PIPE, timeout=timeout, check=True, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\_____\AppData\Local\Programs\Python\Python311\Lib\subprocess.py", line 548, in run with Popen(*popenargs, **kwargs) as process: ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\_____\AppData\Local\Programs\Python\Python311\Lib\subprocess.py", line 1024, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "C:\Users\_____\AppData\Local\Programs\Python\Python311\Lib\subprocess.py", line 1493, in _execute_child hp, ht, pid, tid = _winapi.CreateProcess(executable, args, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FileNotFoundError: [WinError 2] 指定されたファイルが見つかりません。 The above exception was the direct cause of the following exception: Traceback (most recent call last): File "_:\pathpatch3d_demo.py", line 51, in <module> text3d(ax, (1, 5, 0), File "_:\pathpatch3d_demo.py", line 28, in <module> text_path = TextPath((0, 0), s, size=size, usetex=usetex) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\_____\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib\textpath.py", line 366, in __init__ *text_to_path.get_text_path(prop, s, ismath=ismath), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\_____\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib\textpath.py", line 113, in get_text_path glyph_info, glyph_map, rects = self.get_glyphs_tex(prop, s) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\kamif\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib\textpath.py", line 230, in get_glyphs_tex dvifile = TexManager().make_dvi(s, self.FONT_SCALE) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\kamif\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib\texmanager.py", line 300, in make_dvi cls._run_checked_subprocess( File "C:\Users\kamif\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib\texmanager.py", line 259, in _run_checked_subprocess raise RuntimeError( RuntimeError: Failed to process string with tex because latex could not be found 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\texmanager.py", line 255, in _run_checked_subprocess
report = subprocess.check_output(
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\______\AppData\Local\Programs\Python
\Python311\Lib\subprocess.py", line 466, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\______\AppData\Local\Programs\Python
\Python311\Lib\subprocess.py", line 548, in run
with Popen(*popenargs, **kwargs) as process:
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\______\AppData\Local\Programs\Python
\Python311\Lib\subprocess.py", line 1026, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\______\AppData\Local\Programs\Python
\Python311\Lib\subprocess.py", line 1538, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [WinError 2] 指定されたファイルが見つかりません。
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "M:\______\pathpatch3d_demo.py", line 51, in
Python 3.12.0 (matplotlib 3.8.1) では、下記のようなエラーがあり、実行できない。
Traceback (most recent call last):
File "C:\Program Files\Python312\Lib\site-packages\matplotlib\texmanager.py",
line 250, in _run_checked_subprocess
report = subprocess.check_output(
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python312\Lib\subprocess.py", line 466, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python312\Lib\subprocess.py", line 548, in run
with Popen(*popenargs, **kwargs) as process:
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python312\Lib\subprocess.py", line 1026, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Program Files\Python312\Lib\subprocess.py", line 1538, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [WinError 2] 指定されたファイルが見つかりません。
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "E:\_____\pathpatch3d_demo.py", line 51, in
このサンプルは、LATeX を利用しているが、当方の環境には LATeX がインストールされていないので、確認できない。 |
|
mplot3d_Examples code: pathpatch3d_demo.py mplot3d-pathpatch3d-py |
|