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

วิธีซ่อนแกนแต่เก็บป้ายชื่อแกนไว้ใน 3D Plot ด้วย Matplotlib


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

  • กำหนดขนาดรูปภาพและปรับระยะห่างระหว่างและรอบๆ subplots
  • สร้าง figure ใหม่หรือเปิดใช้งาน figure ที่มีอยู่
  • เพิ่ม axes เข้าไปใน figure เป็นส่วนหนึ่งของ subplot arrangement
  • สร้าง x, y, z, dx, dy และ dz ด้วย numpy
  • ใช้ bar3d() พล็อตแท่ง 3 มิติ
  • ซ่อน axes โดยกำหนด tuple สีเท่ากับสี axes
  • ตั้งค่าสีพื้นหลังและเส้นของ x, y, z axes plane ให้ตรง tuple สี
  • ตั้งค่า ticks ว่างให้แกน x, y, z
  • ตั้งค่าป้ายชื่อแกน x, y, z
  • ใช้เมธอด 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()
ax = fig.add_subplot(111, projection='3d')
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [5, 6, 7, 8, 2, 5, 6, 3, 7, 2]
z = np.zeros(10)
dx = np.ones(10)
dy = np.ones(10)
dz = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
ax.bar3d(x, y, z, dx, dy, dz, color="green")
color_tuple = (1.0, 1.0, 1.0, 0.0)
ax.w_xaxis.set_pane_color(color_tuple)
ax.w_yaxis.set_pane_color(color_tuple)
ax.w_zaxis.set_pane_color(color_tuple)
ax.w_xaxis.line.set_color(color_tuple)
ax.w_yaxis.line.set_color(color_tuple)
ax.w_zaxis.line.set_color(color_tuple)
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])
ax.set_xlabel('X-Axis')
ax.set_ylabel('Y-Axis')
ax.set_zlabel('Z-Axis')
plt.show()

ผลลัพธ์

วิธีซ่อนแกนแต่เก็บป้ายชื่อแกนไว้ใน 3D Plot ด้วย Matplotlib