วิธีปรับตำแหน่งหัวเรื่องใน matplotlib


คุณสามารถใช้วิธีใดวิธีหนึ่งต่อไปนี้เพื่อปรับตำแหน่งของชื่อเรื่องในพล็อต Matplotlib:

 #adjust title position using 'loc' argument (left, center, right)
plt. title (' My Title ', loc=' right ')

#adjust title position using x and y coordinates
plt. title (' My Title ', x= 0.5 , y= 1.1 ) 

ตัวอย่างต่อไปนี้แสดงวิธีการใช้แต่ละวิธีในทางปฏิบัติ

วิธีที่ 1: ปรับตำแหน่งชื่อเรื่องโดยใช้ “loc”

รหัสต่อไปนี้แสดงวิธีการปรับตำแหน่งของชื่อเรื่องใน Matplotlib โดยใช้อาร์กิวเมนต์ loc

 import matplotlib. pyplot as plt

#define x and y
x = [1, 4, 10]
y = [5, 9, 27]

#create plot of x and y
plt. plot (x, y)

#add title
plt. title (' My Title ', loc=' left ') 

หมายเหตุ: คุณสามารถใช้อาร์กิวเมนต์ loc เพื่อระบุตำแหน่งหนึ่งในสามตำแหน่ง: ซ้าย ตรงกลาง หรือขวา ตำแหน่งเริ่มต้นคือกึ่งกลาง

วิธีที่ 2: ปรับตำแหน่งชื่อเรื่องโดยใช้พิกัด (x,y)

รหัสต่อไปนี้แสดงวิธีการปรับตำแหน่งของชื่อเรื่องใน Matplotlib โดยใช้พิกัด x และ y

 import matplotlib. pyplot as plt

#define x and y
x = [1, 4, 10]
y = [5, 9, 27]

#create plot of x and y
plt. plot (x, y)

#add title
plt. title (' My Title ', x= 0.5 , y= 1.1 ) 

คุณยังสามารถเลือกที่จะระบุพิกัดใดพิกัดหนึ่งก็ได้ ตัวอย่างเช่น คุณสามารถระบุเฉพาะพิกัด y:

 import matplotlib. pyplot as plt

#define x and y
x = [1, 4, 10]
y = [5, 9, 27]

#create plot of x and y
plt. plot (x, y)

#add title
plt. title (' My Title ', y= 1.3 ) 

แหล่งข้อมูลเพิ่มเติม

วิธีเพิ่มขนาดพล็อตใน Matplotlib
วิธีการตั้งค่าช่วงแกนใน Matplotlib
วิธีการตั้งค่าแกน X ใน Matplotlib
วิธีเปลี่ยนจำนวนเห็บใน Matplotlib

เพิ่มความคิดเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องข้อมูลจำเป็นถูกทำเครื่องหมาย *