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

วิธีพลอต Histograms จาก DataFrames ใน Pandas ด้วย Matplotlib

ในการพลอต histograms ต่อ Pandas/Matplotlib เราสามารถทำตามขั้นตอนต่อไปนี้:

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots
  • สร้างข้อมูลตารางที่อาจมีประเภทต่างกัน (potentially heterogeneous tabular data) ด้วย Pandas dataframe
  • ใช้ dataframe สร้าง histogram
  • เพื่อแสดงรูป ใช้เมธอด show()

ตัวอย่าง

from matplotlib import pyplot as plt
import pandas as pd

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

df = pd.DataFrame({'a': [1, 1, 1, 1, 3],
                   'b': [1, 1, 2, 1, 3],
                   'c': [2, 2, 2, 1, 3],
                   'd': [2, 1, 2, 1, 3]})

df.hist()
plt.show()

ผลลัพธ์

วิธีพลอต Histograms จาก DataFrames ใน Pandas ด้วย Matplotlibวิธีพลอต Histograms จาก DataFrames ใน Pandas ด้วย Matplotlib