
使用 Matplotlib 時,您可以修改繪圖的不同屬性——顏色、大小、標簽、標題等。
在本文中,您將了解 Matplotlib 中的圖例是什么,以及如何使用它的一些參數(shù)來使您的繪圖更具相關(guān)性。
然后,如何修復(fù) Windows Media Player 添加到媒體庫不起作用您將學(xué)習(xí)如何使用以下方法更改 Matplotlib 圖例的字體大?。?/p>
這fontsize。
參數(shù)prop。
什么是 Matplotlib 中的圖例?
圖例是一個 Matplotlib 函數(shù),用于描述構(gòu)成圖形的元素。
考慮下圖:
import matplotlib.pyplot as plt
# create a plot
x = [1, 4, 6, 8]
y = [2, 5, 6, 2]
plt.plot(x, y)
plt.legend(["Data"], loc="upper right")
plt.show()

帶有圖例的 matplotlib 圖
在上圖中,我們使用legend. upper right“數(shù)據(jù)”的描述被分配給圖例,并使用參數(shù)值放置在圖表的右上角loc。
使用該legend功能,您可以為圖形的每一行分配不同的描述。
這是一個例子:
import matplotlib.pyplot as plt
age = [1, 4, 6, 8]
number = [4, 5, 6, 2, 1]
plt.plot(age)
plt.plot(number)
plt.legend(["age", "number"], loc ="upper right")
plt.show()

具有不同圖例描述的兩條折線圖
在上圖中,我們使用函數(shù)legend來描述圖中的每條線。
這使得查看圖表的任何人都更容易知道圖中的藍線表示age而橙色線表示。number
您可以使用以下參數(shù)值更改圖例的位置loc:
best
upper right
upper left
lower left
lower right
right
center left
center right
lower center
upper center
center
如何使用 Matplotlib 更改圖例字體大小fontsize參數(shù)更改 Matplotlib 中的圖例字體大小
您可以通過為參數(shù)指定字體大小值來更改 Matplotlib 圖例的字體大小fontsize。
這是默認圖例字體大小的樣子:
import matplotlib.pyplot as plt
age = [1, 4, 6, 8]
number = [4, 5, 6, 2, 1]
plt.plot(age)
plt.plot(number)
plt.legend(["age", "number"], loc ="upper right")
plt.show()

具有默認圖例字體大小的 matplotlib 圖
這是另一個包含參數(shù)的代碼示例fontsize:
import matplotlib.pyplot as plt
age = [1, 4, 6, 8]
number = [4, 5, 6, 2, 1]
plt.plot(age)
plt.plot(number)
plt.legend(["age", "number"], fontsize="20", loc ="upper left")
plt.show()
這是圖例的樣子:

使用 fontsize 參數(shù)的 matplotlib 圖例大小
我們?yōu)閰?shù)指定了 20 的字體大小fontsize,以獲得上圖中的圖例大?。篺ontsize="20"。
您還會注意到使用參數(shù)將圖例放置在圖表的左上角loc。
如何使用prop參數(shù)更改 Matplotlib 中的圖例字體大小
另一種改變圖例字體大小的方法是使用函數(shù)legend的prop參數(shù)。
下面是如何使用它:
import matplotlib.pyplot as plt
age = [1, 4, 6, 8]
number = [4, 5, 6, 2, 1]
plt.plot(age)
plt.plot(number)
plt.legend(["age", "number"], prop = { "size": 20 }, loc ="upper left")
plt.show()
使用該prop參數(shù),我們指定了 20: 的字體大小prop = { "size": 20 }。
這是輸出:

使用 prop 參數(shù)的 matplotlib 圖例大小
概括
在本文中,我們討論了legendMatplotlib 中的函數(shù)。它可用于描述構(gòu)成圖形的元素。
我們首先看到了 Matplotlib 中的圖例,以及一些示例來展示其基本用法和參數(shù)。文章來源:http://www.zghlxwxcb.cn/news/detail-445232.html
然后我們看到了如何使用fontsize和prop參數(shù)來更改 Matplotlib 圖例的字體大小。文章來源地址http://www.zghlxwxcb.cn/news/detail-445232.html
到了這里,關(guān)于如何在 Matplotlib 中更改圖例字體大小的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!