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

วิธีสร้างฮิสโตแกรมสะสมย้อนกลับใน Matplotlib


เพื่อสร้างฮิสโตแกรมสะสมย้อนกลับ (reverse-order cumulative histogram) ใน Matplotlib ให้ใช้ cumulative = -1 ในเมธอด hist()

  • กำหนดขนาดรูปและปรับระยะห่างระหว่างและรอบซับพล็อต
  • สร้างรายการจุดข้อมูล
  • พล็อตฮิสโตแกรมพร้อม data และ cumulative = -1
  • ใช้ show() เพื่อแสดงรูป

ตัวอย่าง

from matplotlib import pyplot as plt

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

data = [1, 2, 2, 3, 1, 4, 3, 0, 1, 3, 0]
plt.hist(data, edgecolor='black', align="mid", cumulative=-1)
plt.show()

ผลลัพธ์

วิธีสร้างฮิสโตแกรมสะสมย้อนกลับใน Matplotlib