Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Vẽ nhiều boxplot trên một đồ thị trong Pandas hoặc Matplotlib


Để vẽ nhiều boxplot trên một đồ thị trong Pandas hoặc Matplotlib, thực hiện các bước sau:

Các bước

  • Thiết lập kích thước hình và padding.
  • Tạo DataFrame Pandas hai cột.
  • Vẽ DataFrame bằng plot() với kind='box'.
  • Hiển thị hình bằng show().

Ví dụ

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
data = pd.DataFrame({"Box1": np.random.rand(10), "Box2": np.random.rand(10)})
ax = data[['Box1', 'Box2']].plot(kind='box', title='boxplot')
plt.show()

Kết quả

Hai boxplot cạnh nhau cho Box1 và Box2, hiển thị phân phối quartile, median, outliers.