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

วิธีหลีกเลี่ยง error bars ทับซ้อนใน matplotlib

ในการหลีกเลี่ยง error bars ทับซ้อนใน matplotlib สามารถทำตามขั้นตอนดังนี้:

ขั้นตอน

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots

  • สร้างรายการชื่อ

  • ได้ข้อมูล y1, y2 และ errors ye1, ye2

  • สร้าง figure และชุด subplots

  • สร้าง mutable 2D affine transformation trans1 และ trans2

  • วาด y versus x พร้อม errorbars

  • แสดงรูปด้วยเมธอด show()

ตัวอย่าง

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.transforms import Affine2D
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = ['Jack', 'James', 'Tom', 'Garry']
y1, y2 = np.random.randn(2, len(x))
ye1, ye2 = np.random.rand(2, len(x))*4+0.3
fig, ax = plt.subplots()
trans1 = Affine2D().translate(-0.1, 0.0) + ax.transData
trans2 = Affine2D().translate(0.1, 0.0) + ax.transData
er1 = ax.errorbar(x, y1, yerr=ye1, marker="*", linestyle="none", transform=trans1)
er2 = ax.errorbar(x, y2, yerr=ye2, marker="o", linestyle="none", transform=trans2)
plt.show()

ผลลัพธ์

จะแสดง error bars สองชุด ขยับซ้าย-ขวา 0.1 หน่วย ไม่ทับซ้อนกัน

วิธีหลีกเลี่ยง error bars ทับซ้อนใน matplotlib