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

วิธีแสดงค่าเฉลี่ย (Mean) ใน Box Plot ด้วย Python Matplotlib


ใช้ showmeans=True ในอาร์กิวเมนต์ของเมธอด boxplot()

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

  • กำหนดขนาดรูปภาพและปรับระยะห่างระหว่างและรอบๆ subplots
  • สร้างชุดข้อมูลสุ่ม
  • สร้าง figure ใหม่หรือเปิดใช้งาน figure ที่มีอยู่ด้วย figure()
  • เพิ่ม axes เข้าไปใน figure ปัจจุบันเป็น subplot arrangement
  • สร้าง boxplot ด้วย boxplot() พร้อม showmeans=True
  • ใช้เมธอด show() เพื่อแสดงรูปภาพ

ตัวอย่างโค้ด

import matplotlib.pyplot as plt
import numpy as np

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

data = np.random.rand(100, 5)
fig = plt.figure()
ax = fig.add_subplot(111)
bp = ax.boxplot(data, 'd', showmeans=True)
plt.show()

ผลลัพธ์

วิธีแสดงค่าเฉลี่ย (Mean) ใน Box Plot ด้วย Python Matplotlib