Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีวาด multivariate function ใน Python Matplotlib

ในการวาด multivariate function ใน Python สามารถทำตามขั้นตอนดังนี้:

ขั้นตอน

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots

  • สร้างข้อมูล x, y, z สุ่มด้วย numpy

  • สร้าง figure และชุด subplots

  • สร้าง scatter plot ด้วย x, y, z

  • สร้าง colorbar สำหรับ ScalarMappable instance s

  • แสดงรูปด้วยเมธอด show()

ตัวอย่าง

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
def func(x, y):
    return 3 * x + 4 * y - 2 + np.random.randn(30)
x, y = np.random.randn(2, 30)
y *= 100
z = func(x, y)
fig, ax = plt.subplots()
s = ax.scatter(x, y, c=z, s=100, marker='*', cmap='plasma')
fig.colorbar(s)
plt.show()

ผลลัพธ์

จะแสดง scatter plot 30 จุด สีตามค่า z = 3x + 4y - 2 + noise colormap plasma

วิธีวาด multivariate function ใน Python Matplotlib