1. y=f(x)
#y=f(x)
from numpy import *
import matplotlib.pyplot as plt
xrange = [0,10]
def f(x):
return sin(x)
x_line = arange(xrange[0], xrange[1], 0.1) # 0.1 간격으로 점을 찍는다
y_line = f(x_line)
plt.plot(x_line, y_line)
plt.xlabel('x'); plt.ylabel('y'); plt.title('y=f(x)')
plt.show()
2. f(x,y)=0
from numpy import *
import matplotlib.pyplot as plt
def f(x,y):
return 0.01*x**2+0.008*x*y-0.013*y**2+0.15*x+0.003*y+1.0097
x = arange(-10.0,10.0,0.1)
y = arange(-10.0,10.0,0.1)
X, Y = meshgrid(x,y)
plt.contour(x, y, f(X, Y), [0]);
# label(or legend)을 넣으려면
# cs = plt.contour(x, y, f(X, Y), [0]); plt.clabel(cs, fmt='%.1f');
3. z=f(x,y) in 3D
# prompt: plot z=f(x,y) in 3d
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
def f(x, y):
# Replace this with your desired function
return 0.01*x**2+0.008*x*y-0.013*y**2+0.15*x+0.003*y+1.0097
# Create the figure and axes object for the 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Generate the x, y, and z data
x = np.arange(-10, 10, 0.5)
y = np.arange(-10, 10, 0.5)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
# Plot the surface
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm)
# Add labels and a colorbar
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
fig.colorbar(surf, shrink=0.5, aspect=5)
# Show the plot
plt.show()
4. z=f(x,y) in 2D by contour
import numpy as np
import matplotlib.pyplot as plt
from math import *
fig = plt.figure()
ax = fig.add_subplot(111)
levels = np.arange(0,3,0.1)
x_cut = 100
y_cut = 100
x = np.linspace(-10.0, 10.0, x_cut)
y = np.linspace(-10.0, 10.0, y_cut)
X, Y = np.meshgrid(x,y)
F = np.zeros((y_cut, x_cut), dtype='float32')
for i in range(x_cut):
for j in range(y_cut):
a = x[i]
b = y[j]
F[i][j] = sqrt((sin(a/2))**2 + (sin(((a/2)+(sqrt(3)*b/2))/2))**2 + (sin(((-a/2)+(sqrt(3)*b/2))/2))**2 + \
sqrt((((sin(a/2))**2 - (sin(((a/2)+(sqrt(3)*b/2))/2))**2)**2+ \
((sin(a/2))**2 - (sin(((-a/2)+(sqrt(3)*b/2))/2))**2)**2+ \
((sin(((a/2)+(sqrt(3)*b/2))/2))**2-(sin(((-a/2)+(sqrt(3)*b/2))/2))**2)**2)/2))
CS = plt.contour(X,Y,F,levels)
ax.set_aspect('equal', adjustable='box')
for i in range(len(levels)):
CS.collections[i].set_label(levels[i])
plt.colorbar()
plt.show()
'잡다한 코드' 카테고리의 다른 글
wolfram alpha의 행렬 지수 연산의 문제 (0) | 2024.10.06 |
---|---|
파이썬으로 curve fitting 및 R^2 값 구하기 (0) | 2024.10.06 |