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

วิธีสร้างฮิสโตแกรม 3 มิติใน Python ด้วย Matplotlib


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

  • กำหนดขนาดรูปภาพและปรับระยะห่างระหว่างและรอบๆ subplots
  • สร้าง figure ใหม่หรือเปิดใช้งาน figure ที่มีอยู่ด้วย figure()
  • เพิ่ม axes เข้าไปใน figure ปัจจุบันเป็น subplot arrangement
  • สร้างข้อมูล x3, y3 และ z3 ด้วย numpy
  • สร้างข้อมูล dx, dy และ dz ด้วย numpy
  • ใช้ bar3d() พล็อตแท่ง 3 มิติ
  • ซ่อนแกนด้วย axis('off')
  • ใช้เมธอด show() เพื่อแสดงรูปภาพ

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

import numpy as np
from matplotlib import pyplot as plt

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

fig = plt.figure()
ax1 = fig.add_subplot(111, projection='3d')
x3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y3 = [5, 6, 7, 8, 2, 5, 6, 3, 7, 2]
z3 = np.zeros(10)
dx = np.ones(10)
dy = np.ones(10)
dz = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
ax1.bar3d(y3, x3, z3, dx, dy, dz, color="red")
ax1.axis('off')
plt.show()

ผลลัพธ์

วิธีสร้างฮิสโตแกรม 3 มิติใน Python ด้วย Matplotlib