はじめに
ipywidgetsのIntSliderをつかって、matplotlibの3Dプロットのアスペクト比をインタラクティブに変更する方法について説明する。
コード

解説
モジュールのインポートなど
jupyter labでipywidgetsを表示するために%matplotlib widgetとする。
バージョン
データの生成
媒介変数で球面のデータを作成する。
surface plotによる球の表示
plot_surfaceでx,y,zのデータを3Dグラフ上にsurface plotとして表示する。
IntSliderの設定
x_a,y_a,z_aを1から10の範囲で変化させることができるようにする。
IntSliderを変化させたときの動作
set_box_aspectでアスペクト比をセットする。なお、デフォルト値は4:4:3となっており、set_box_aspectはversion 3.3以上で使うことができる。
widgetの表示

IntSliderを変化させたときの3Dプロットの変化
4:4:4の場合
3:3:3のようにしても同じ図が得られる。

8:3:8の場合

8:8:3の場合

参考
What's new? — Matplotlib 3.3.0 documentation
mpl_toolkits.mplot3d.axes3d.Axes3D — Matplotlib 3.3.0 documentation
コメント