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

วิธีเพิ่มขนาดฟอนต์ legend ใน Seaborn plot ด้วย Matplotlib


ปรับขนาดตัวอักษร legend ใน Seaborn

ใช้ตัวแปร fontsize ในอาร์กิวเมนต์ของเมธอด legend()

ขั้นตอน

  1. สร้าง DataFrame ด้วย Pandas (คีย์: number, count, select)
  2. วาด barplot ด้วย barplot() ของ Seaborn
  3. กำหนดตัวแปร fontsize
  4. ใช้ legend() พร้อม fontsize ในอาร์กิวเมนต์
  5. แสดงด้วย show()

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

import pandas
import matplotlib.pylab as plt
import seaborn as sns

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

df = pandas.DataFrame(dict(
    number=[2, 5, 1, 6, 3],
    count=[56, 21, 34, 36, 12],
    select=[29, 13, 17, 21, 8]
))

bar_plot1 = sns.barplot(x='number', y='count', data=df, label="count", color="red")
bar_plot2 = sns.barplot(x='number', y='select', data=df, label="select", color="green")

fontsize = 20
plt.legend(loc="upper right", frameon=True, fontsize=fontsize)
plt.show()

ผลลัพธ์

วิธีเพิ่มขนาดฟอนต์ legend ใน Seaborn plot ด้วย Matplotlib