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

วิธีทำให้ Colorbar แนวนอนใน Python ด้วย Matplotlib

ในการทำให้ colorbar แนวนอนใน Python เราสามารถใช้ orientation="horizontal" ในอาร์กิวเมนต์

ขั้นตอน

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots
  • สร้างจุดข้อมูล x, y และ z แบบสุ่มด้วย numpy
  • สร้าง figure และชุด subplots
  • ใช้เมธอด scatter() พลอต x, y และ z
  • สร้าง colorbar สำหรับ ScalarMappable instance พร้อม orientation แนวนอน
  • เพื่อแสดงรูป ใช้เมธอด show()

ตัวอย่าง

import numpy as np
import matplotlib.pyplot as plt

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

x, y, z = np.random.rand(3, 50)
f, ax = plt.subplots()
points = ax.scatter(x, y, c=z, s=50, cmap="plasma")
f.colorbar(points, orientation="horizontal")
plt.show()

ผลลัพธ์

วิธีทำให้ Colorbar แนวนอนใน Python ด้วย Matplotlib