matplotlib 1

matplotlibで図を作成します。

準備

matplotlibを使うための準備をします。環境はMacです。

LLVMコンパイラ、リンカ、などをインストールします。


> xcode-select --install

Homebrewをインストールします。
Homebrew


> /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

python3などをインストールします。python3のインストールにpip3は含まれていたと思います。
(2019年12月のときには、pythonが2.x系だったと思います。今(2021年5月)は、3.x系が標準と思います。ですので、brew install python3が不要と思います。)


> brew install python3
(> brew install pip3)
> pip3 install numpy
> pip3 install matplotlib

Big Surにしたので、python3.9を使います。


> xcode-select --install
> brew install python3

一次関数の図を作成する

下記の一次関数のグラフを描きます。

$$ y = x $$

公式ホームページでstepを0.1なんかにするならnumpy.arangeよりもnumpy.linspaceの方が良い、と書かれていますが、ここではお行儀の悪い方で書いています。
numpy.arange

linear_function_001.py

import numpy as np
import matplotlib.pyplot as pyplot

## When using a non-integer step, such as 0.1, the results will often not be consistent.
## It is better to use numpy.linspace for these cases.

x = np.arange( 0, 100, 0.1 )
y = x

pyplot.plot( x,y );

pyplot.xlabel("x")
pyplot.ylabel("y", rotation=0)

## matplotlib.pyplot.ylim( bottom, top )
pyplot.ylim(0, 200)

pyplot.savefig('001.png')

ちなみに、

matplotlib.pyplot.xlim( left, right )

です。

001

下記の一次関数のグラフを描きます。

$$ y = 2x $$

linear_function_002.py

import numpy as np
import matplotlib.pyplot as pyplot

x = np.arange( 0, 100, 0.1 )
y = 2 * x

pyplot.plot( x,y );

pyplot.xlabel("x")
pyplot.ylabel("y", rotation=0)

pyplot.savefig('005.png')
002

二次関数の図を作成する

下記の二次関数のグラフを描きます。

$$ y = x^2 $$

quadratic_function_001.py

import numpy as np
import matplotlib.pyplot as pyplot

x = np.arange( 0, 100, 0.1 )
y = x * x

pyplot.plot( x,y );

pyplot.xlabel("x")
pyplot.ylabel("y", rotation=0)

pyplot.savefig('003.png')
003

下記の二次関数のグラフを描きます。

$$ y = \frac{1}{2} x^2 $$

quadratic_function_002.py

import numpy as np
import matplotlib.pyplot as pyplot

x = np.arange( 0, 100, 0.1 )
y = (x * x) / 2

pyplot.plot( x,y );

pyplot.xlabel("x")
pyplot.ylabel("y", rotation=0)

pyplot.ylim(0, 10000)

pyplot.savefig('004.png')
004

三次元の図を作成する

z軸方向について三次関数のグラフを描きます。(640×480に入らなくなるので、100000で割ってます。)

$$ z = x^3 $$

https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.figure.html

ここの一番下にあるExamplesを参考にするとほとんど何でもできると思います。

three_dimensional_space_001.py

import numpy as np
import matplotlib.pyplot as pyplot
from mpl_toolkits.mplot3d import Axes3D

fig = pyplot.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.arange(-1000, 1000, 0.1)
y = np.arange(-500, 500, 0.05)

z = x * x * x / 100000
ax.plot(x,y,z);

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

pyplot.savefig('001.png')
005

z軸方向について二次関数のグラフを描きます。(640×480に入らなくなるので、1000で割ってます。)

three_dimensional_space_002.py

import numpy as np
import matplotlib.pyplot as pyplot
from mpl_toolkits.mplot3d import Axes3D

fig = pyplot.figure()
ax = fig.add_subplot(111, projection='3d')
# x = np.arange(-1000, 1000, 0.1)
# y = np.arange(-500, 500, 0.05)
x = np.arange(0, 1000, 0.1)
y = np.arange(0, 500, 0.05)

z = x * x / 1000

ax.plot(x,y,z);

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

pyplot.savefig('002.png')
006

広告

IT開発関連書とビジネス書が豊富な翔泳社の通販『SEshop』
さくらのレンタルサーバ
ムームードメイン
Oisix(おいしっくす)
らでぃっしゅぼーや
珈琲きゃろっと
エプソムソルト


PythonでCSVファイルを処理する 1
WindowsでPythonを使う
matplotlib 1


«       »