Latihan Membuat Grafik
Latihan Membuat Grafik
digunakan integral:
Area = ∫ 𝑓 𝑥 𝑑𝑥 = ∫ 2𝑥 +3x+4
Area =
dx = 1
x = np.arange(0,10,dx)
a = 2
b = 3
c = 4
y = [a*x1**2 + b*x1 + c for x1 in x]
y1 = [a*(x1-0.5*dx)**2+b*(x1-0.5*dx)+c for x1 in x]
#plot grafik
plt.plot(x, y1, color = 'red', linewidth=2, label='y=2x^2+3x+4')
plt.title('Menghitung Luas di Bawah
Kurva',fontsize=14,fontstyle='italic')
plt.xlabel('x')
plt.ylabel('y')
plt.legend(loc='upper left', fontsize=12)
plt.xticks(np.arange(min(x), max(x)+1, dx))
pos = (4.5, 5.5, 6.5, 7.5, 8.5)
h = [y[4],y[5],y[6],y[7],y[8]]
plt.bar(pos, h, width=dx, fill=False, hatch ='.')
#menghitung luas
Tot_A = 0
for i in range(4,9):
A = y1[i+1] * dx
Tot_A = Tot_A + A
plt.show()
Untuk memplot grafik 3 dimensi pada Matplotlib
dapat dilakukan dengan
mengimport mplot3d toolkit, yang sudah terinstall
bersama instalasi Matplotlib, dengan perintah :
from mpl_toolkits import
Dan dilanjutkan dengan mengimport fungsi grafik
seperti biasa:
import matplotlib.pyplot as plt
mimport numpy as np
Kemudian memanggil fungsi 3D:
Fig= plt.figure()
ax= plt.axes (projection='3d')
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes(projection='3d')
plt.show()
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
# 3D contour
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z, 50, cmap='binary')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z');
ax.set_title('3D Contour');
ax.view_init(60, 35)
plt.show()
# Wireframe
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_wireframe(X, Y, Z, color='black')
ax.set_title('wireframe’)
plt.show()
# Surface
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
cmap='viridis', edgecolor='none')
ax.set_title('surface');
r = np.linspace(0, 6, 20)
theta = np.linspace(-0.9 * np.pi, 0.8 * np.pi, 40)
r, theta = np.meshgrid(r, theta)
X = r * np.sin(theta)
Y = r * np.cos(theta)
Z = f(X, Y)
ax = plt.axes(projection='3d')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
cmap='viridis', edgecolor='none');
plt.show()