เพื่อลดความหนาแน่นฟักไข่ใน Matplotlib เราสามารถทำตามขั้นตอนต่อไปนี้ -
-
กำหนดขนาดรูปและปรับช่องว่างภายในระหว่างและรอบๆ แผนผังย่อย
-
สร้างคลาสฟักแนวนอนที่ปรับแต่งได้เพื่อแทนที่ความหนาแน่น
-
ต่อท้ายคลาสฟักแนวนอน
-
สร้างตัวเลขใหม่หรือเปิดใช้งานตัวเลขที่มีอยู่
-
เพิ่ม 'ax1' ไปที่ร่างซึ่งเป็นส่วนหนึ่งของการจัดโครงเรื่องย่อย
-
ทำรายการจุดข้อมูล
-
สร้างพล็อตแท่งด้วย x และ ย จุดข้อมูลด้วย hatch='o', color='green' และ edgecolor='red' .
-
หากต้องการแสดงรูป ให้ใช้ show() วิธีการ
ตัวอย่าง
from matplotlib import pyplot as plt, hatch
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
class MyHorizontalHatch(hatch.HorizontalHatch):
def __init__(self, hatch, density):
char_count = hatch.count('o')
if char_count > 0:
self.num_lines = int((1.0 / char_count) * density)
else:
self.num_lines = 0
self.num_vertices = self.num_lines * 2
super().__init__(hatch, density)
hatch._hatch_types.append(MyHorizontalHatch)
fig = plt.figure()
ax1 = fig.add_subplot(111)
x = [3, 6, 1]
y = [4, 6, 1]
ax1.bar(x, y, color='green', edgecolor='red', hatch="o", lw=1., zorder=0)
plt.show() ผลลัพธ์
