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

วิธีเพิ่มเท็กซ์เจอร์ (Textures) ให้แท่งและก้นพายใน Matplotlib


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

  • กำหนดขนาดรูปภาพและปรับระยะห่างระหว่างและรอบๆ subplots
  • สร้าง figure ใหม่หรือเปิดใช้งาน figure ที่มีอยู่ด้วย figure()
  • เพิ่ม axes เข้าไปใน figure เป็นส่วนหนึ่งของ subplot arrangement
  • สร้างรายการ hatches แท่งสามารถเติมด้วย hatches ได้
  • สร้างจำนวนแท่งเท่ากับจำนวน hatches
  • ใช้ bar() พล็อตแท่งพร้อม hatch ต่างกัน
  • ใช้เมธอด 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)
textures = ["//", "*", "o", "d", "."]
for i in range(len(textures)):
    ax.bar(i, np.random.randint(1, 5), color="green", edgecolor="black", alpha=0.3, hatch=textures[i])
plt.show()

ผลลัพธ์

วิธีเพิ่มเท็กซ์เจอร์ (Textures) ให้แท่งและก้นพายใน Matplotlib