今回は、Python で contour 関数と Matpotlib を使って等高線プロットを作成する方法について学びます。
私たちは、異なるタイプのプロット関数と、それらを通して作成される異なるタイプのプロットを見ていきます。
また、それに沿った詳細な説明とともに、コードを見ていきます。
輪郭プロットって何?
pip install matplotlib |
等高線は、3次元の曲面を曲線と関節で2次元に表現したものです。
等高線プロットを扱うには、matplotlib と NumPy という2つのライブラリが必要です。
これらをインストールしましょう。
matplotlib は Python ベースのプロット用ライブラリで、グラフやプロットの作成に使われます。
matplotlibをインストールするには、次のコマンドを入力します。
pip install numpy |
等高線プロットを作成するために、もう一つのライブラリであるPython Numpyが必要です。
これをインストールするには、次のコマンドを入力します。
import matplotlib.pyplot as plt
import numpy as nump
|
この記事もチェック:Pythonとmatplotlibでエラーバーのグラフを作成する方法
輪郭プロットを作成する
基本的な要件が整ったので、さっそく等高線プロットを作ってみましょう。
重要なライブラリのインポート
element_ofx = nump.arange(0, 25, 4)
element_ofy = nump.arange(0, 26, 4)
|
X, Y変数の初期化
以下のコードでは、変数 X と Y はプロットのための 3 次元座標で初期化されます。
[grid_ofX, grid_ofY] = nump.meshgrid(element_ofx, element_ofy)
fig, holowplt= plt.subplots(1, 1)
grid_ofZ = nump.cos(grid_ofX / 1) - nump.sin(grid_ofY / 2)
|
2つの変数による輪郭関数Zの作成
holowplt.contour(grid_ofX, grid_ofY, grid_ofZ)holowplt.set_title('Contour Plot')
holowplt.set_xlabel('features of x-axis')
holowplt.set_ylabel('features of y-axis')
plt.show() |
コンターチャートのプロット
import matplotlib.pyplot as plt
import numpy as nump
element_ofx = nump.arange(0, 25, 4)
element_ofy = nump.arange(0, 26, 4)
# This numpy function creates 2-dimensional grid[grid_ofX, grid_ofY] = nump.meshgrid(element_ofx, element_ofy)
# plots 2 graphs in one chartfig, holowplt = plt.subplots(1, 1)
# Mathematical function for contourgrid_ofZ = nump.cos(grid_ofX / 1) - nump.sin(grid_ofY / 2)
# plots contour linesholowplt.contour(grid_ofX, grid_ofY, grid_ofZ)holowplt.set_title('Contour Plot')
holowplt.set_xlabel('features of x-axis')
holowplt.set_ylabel('features of y-axis')
plt.show() |
以下のコードは、シンプルで中空な matplotlib の等高線プロットがどのように作成されるかを示しています。
fillplot.contourf(grid_ofX, grid_ofY, grid_ofZ)fillplot.set_title('Contour Plot')
fillplot.set_xlabel('features of x-axis')
fillplot.set_ylabel('features of y-axis')
|
出力
import matplotlib.pyplot as plt
import numpy as nump
element_ofx = nump.arange(0, 25, 4)
element_ofy = nump.arange(0, 26, 4)
# This numpy function creates 2-dimensional grid[grid_ofX, grid_ofY] = nump.meshgrid(element_ofx, element_ofy)
# plots 2 graphs in one chartfig, fillplot = plt.subplots(1, 1)
# Mathematical function for contourgrid_ofZ = nump.cos(grid_ofX / 1) - nump.sin(grid_ofY / 2)
# plots contour linesfillplot.contourf(grid_ofX, grid_ofY, grid_ofZ)fillplot.set_title('Contour Plot')
fillplot.set_xlabel('features of x-axis')
fillplot.set_ylabel('features of y-axis')
plt.show() |
塗りつぶしコンタープロット
この例では、空洞の等高線プロットではなく、塗りつぶしの等高線プロットを作成します。
塗りつぶしプロットを作成するために、’contourf’関数を使用します。
プログラム全体は、前の例と非常によく似ていますが、若干の変更があります。
コンターチャートのプロット
import numpy as np
import matplotlib.pyplot as plt
|
理解を深めるために、コード全体を見てみましょう。
delta = 0.18
element_ofx = np.arange(1.8, 2.8, delta)
element_ofy = np.arange(1.5, 3.6, delta)
grid_ofX, grid_ofY = np.meshgrid(element_ofx, element_ofy)
grid_ofZ = (np.exp(grid_ofX + grid_ofY))
|
出力
# Importing librariesimport numpy as np
import matplotlib.pyplot as plt
# variable initialisationdelta = 0.18
element_ofx = np.arange(1.8, 2.8, delta)
element_ofy = np.arange(1.5, 3.6, delta)
grid_ofX, grid_ofY = np.meshgrid(element_ofx, element_ofy)
grid_ofZ = (np.exp(grid_ofX + grid_ofY))
# Contour plottingplot = plt.contour(grid_ofX, grid_ofY, grid_ofZ)
grid_format = {}
numscale = ['1', '2', '3', '4', '5', '6', '7']
for lvls, s in zip(plot.levels, numscale):
grid_format[lvls] = s
plt.clabel(plot, plot.levels, inline = True,
fmt = grid_format, fontsize = 10)
plt.title('Contour in Matlab interface')
plt.show() |
輪郭描画に状態ベースのインタフェースを使用する
matplotlib サブモジュールにより、様々なインタフェースで等高線を描画することができます。
このセクションでは、MATLAB インターフェースに似た方法で輪郭を描画する matplotlib のモードについて見ていきます。
このサブモジュールを使ってどのように輪郭を描くか、コードごとに理解していきましょう。
ライブラリのインポート
この例では、これまでの例と同様に、主に matplotlib と Numpy の2つのライブラリを使用します。

変数の初期化

理解を深めるために、コードの全体像を見てみましょう。


まとめ
この記事は、matplotlibを学ぶための良い基礎となります。
すべてのトピックとコンセプトは、読者が簡単にすべての基本をつかむことができるように、理解しやすい方法で提唱されています。
記事全体の概要をよく理解することで、より高度な matplotlib の概念に容易に踏み込むことができます。