ในการพล็อต dataframe ของ Python Pandas ช่วงความเชื่อมั่น 95% เราสามารถทำตามขั้นตอนต่อไปนี้ -
- กำหนดขนาดรูปและปรับช่องว่างภายในระหว่างและรอบๆ แผนผังย่อย
- รับอินสแตนซ์ดาต้าเฟรมของข้อมูลตารางแบบสองมิติ ปรับขนาดได้ และอาจต่างกันได้
- สร้าง dataframe ที่มีสองคอลัมน์ หมวดหมู่ และ หมายเลข .
- ค้นหา ค่าเฉลี่ย และ มาตรฐาน ของ หมวดหมู่ และ หมายเลข .
- พล็อต y กับ x เป็นเส้นและ/หรือเครื่องหมายพร้อมแถบข้อผิดพลาดที่แนบมาด้วย
- หากต้องการแสดงรูป ให้ใช้ show() วิธีการ
ตัวอย่าง
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
df = pd.DataFrame()
df['category'] = np.random.choice(np.arange(10), 1000, replace=True)
df['number'] = np.random.normal(df['category'], 1)
mean = df.groupby('category')['number'].mean()
std = df.groupby('category')['number'].std()
plt.errorbar(mean.index, mean, xerr=0.5, yerr=2*std,
linestyle='--', c='red')
plt.show() ผลลัพธ์
