เพื่อปรับระยะห่างระหว่างเครื่องหมาย (markers) และป้ายกำกับ (labels) ในตำนาน สามารถใช้พารามิเตอร์ labelspacing ในเมธอด legend() ได้
ขั้นตอน
- พลอตเส้นข้อมูลพร้อมกำหนด label1, label2 และ label3
- กำหนดตัวแปรระยะห่างเพื่อเพิ่มหรือลดช่องว่างระหว่างเครื่องหมายและป้ายกำกับ
- ใช้เมธอด legend() พร้อมอาร์กิวเมนต์ labelspacing
- แสดงกราฟด้วยเมธอด show()
ตัวอย่างโค้ด
from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True plt.plot([0, 1], [0, 1.0], label='Label 1') plt.plot([0, 1], [0, 1.1], label='Label 2') plt.plot([0, 1], [0, 1.2], label='Label 3') space = 2 plt.legend(labelspacing=space) plt.show()
ผลลัพธ์
