Loading [MathJax]/extensions/tex2jax.js

[matplotlib 3D] 27. 3D wireframeグラフでX軸方向またはY軸方向 のみのデータを表示

matplotlib 3D

はじめに

matplotlib mplot3dの3D wireframeグラフでX or Y 軸の一方向だけのデータを表示する方法について解説する。

コード

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import axes3d
plt.style.use('ggplot')
plt.rcParams["axes.facecolor"] = 'white'
fig, [ax1, ax2] = plt.subplots(1, 2, figsize=(8, 4), subplot_kw={'projection': '3d'})
# Get the test data
X, Y, Z = axes3d.get_test_data(0.05)
# Give the first plot only wireframes of the type y = c
ax1.plot_wireframe(X, Y, Z, rcount=10, ccount=0)
ax1.set_title("Column (x) count set to 0")
# Give the second plot only wireframes of the type x = c
ax2.plot_wireframe(X, Y, Z, rcount=0, ccount=10)
ax2.set_title("Row (y) count set to 0")
plt.tight_layout()
plt.savefig('one_direction_3dwf.jpg',dpi=120)
plt.show()

解説

モジュールのインポート

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import axes3d

図の作成

fig, [ax1, ax2] = plt.subplots(1, 2, figsize=(8, 4), subplot_kw={'projection': '3d'})

subplot_kw={‘projection’: ‘3d’}は、add_subplot(111,projection=’3d’)とおなじ。

一方向wireframeグラフの作成

ax1.plot_wireframe(X, Y, Z, rcount=10, ccount=0)
ax2.plot_wireframe(X, Y, Z, rcount=0, ccount=10)

ccount, rcountをそれぞれ0とすることで、その方向にデータが非表示となる。

各方向で色を変えて描写

fig, ax = plt.subplots(figsize=(5, 5),subplot_kw={'projection': '3d'})
ax.plot_wireframe(X, Y, Z, rcount=10, ccount=0,color='m')
ax.plot_wireframe(X, Y, Z, rcount=0, ccount=10,color='g')
plt.tight_layout()
plt.savefig('one_direction_3dwf_dc.jpg',dpi=120)
plt.show()
コードをダウンロード(.pyファイル)

コードをダウンロード(.ipynbファイル)

参考

mplot3d tutorial — Matplotlib 2.0.2 documentation
mplot3d example code: wire3d_zero_stride.py — Matplotlib 2.0.2 documentation

コメント