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

วิธีพล็อตฮิสโตแกรม 2 มิติ (2D Histogram) ใน Matplotlib


ขั้นตอนการทำงาน

  • กำหนดขนาดรูปภาพและปรับระยะห่างระหว่างและรอบๆ subplots
  • สร้างข้อมูล x และ y ด้วย numpy
  • สร้าง figure และชุด subplots
  • พล็อต x และ y ด้วยเมธอด hist2d()
  • ตั้งชื่อเรื่องของกราฟ
  • ใช้เมธอด show() เพื่อแสดงรูปภาพ

ตัวอย่างโค้ด

import numpy as np
from matplotlib import pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

x = 2 * np.random.randn(5000)
y = x + np.random.randn(5000)
fig, ax = plt.subplots()
_ = ax.hist2d(x[::10], y[::10])
ax.set_title('2D Histogram')
plt.show()

ผลลัพธ์

วิธีพล็อตฮิสโตแกรม 2 มิติ (2D Histogram) ใน Matplotlib