0
0

Pythonを使った3Dグラフの表示

Posted at

f3という関数を様々な方法で表現する。

数値を色で表現する

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

def f3(x0, x1):
    r = 2 * x0**2 + x1**2
    ans = r * np.exp(-r)
    return ans

xn = 9
x0 = np.linspace(-2, 2, xn)
x1 = np.linspace(-2, 2, xn)
y = np.zeros((len(x0), len(x1)))

for i0 in range(xn):
    for i1 in range (xn):
        y[i1, i0] = f3(x0[i0], x1[i1])

print(x0)
print(x1)
print(np.round(y, 1))

plt.figure(figsize=(3.5, 3))
plt.gray()
plt.pcolor(y)
plt.colorbar()
plt.show()

実行結果

image.png

関数の表面を面で表現する

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

def f3(x0, x1):
    r = 2 * x0**2 + x1**2
    ans = r * np.exp(-r)
    return ans

xn = 9
x0 = np.linspace(-2, 2, xn)
x1 = np.linspace(-2, 2, xn)
y = np.zeros((len(x0), len(x1)))

for i0 in range(xn):
    for i1 in range (xn):
        y[i1, i0] = f3(x0[i0], x1[i1])

print(x0)
print(x1)
print(np.round(y, 1))

xx0, xx1 = np.meshgrid(x0, x1)

print(xx0)
print(xx1)

plt.figure(figsize=(5, 3.5))
ax = plt.subplot(1, 1, 1, projection='3d')
ax.plot_surface(xx0, xx1, y, rstride=1, cstride=1, alpha=0.3, color='blue', edgecolor='black')
ax.set_zticks((0, 0.2))
ax.view_init(75, -95)
plt.show()

実行結果

image.png

等高線で表示する

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

def f3(x0, x1):
    r = 2 * x0**2 + x1**2
    ans = r * np.exp(-r)
    return ans

xn = 50
x0 = np.linspace(-2, 2, xn)
x1 = np.linspace(-2, 2, xn)

y = np.zeros((len(x0), len(x1)))

for i0 in range(xn):
    for i1 in range (xn):
        y[i1, i0] = f3(x0[i0], x1[i1])

xx0, xx1 = np.meshgrid(x0, x1)

plt.figure(figsize=(4, 4))
cont = plt.contour(xx0, xx1, y, 5, colors='black')
cont.clabel(fmt='%3.2f', fontsize=8)
plt.xlabel('$x_0$', fontsize=14)
plt.ylabel('$x_1$', fontsize=14)
plt.show()

実行結果

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0