Matplotlib Label
We can use the **xlabel()** and **ylabel()** methods to set the labels for the x-axis and y-axis.
## Example
```python
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1,2,3,4])
y = np.array([1,4,9,16])
plt.plot(x, y)
plt.xlabel("x - label")
plt.ylabel("y - label")
plt.show()
The output is as follows:
!(#)
### Title
We can use the **title()** method to set the title.
## Example
```python
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1,2,3,4])
y = np.array([1,4,9,16])
plt.plot(x, y)
plt.title(" TEST TITLE")
plt.xlabel("x - label")
plt.ylabel("y - label")
plt.show()
The output is as follows:
!(#)
### Displaying Chinese Characters in Plots
Matplotlib does not support Chinese characters by default. We can use the following simple method to resolve this.
Here we use Source Han Sans, an open-source font developed by Adobe and Google.
Official website: [https://source.typekit.com/source-han-serif/cn/](https://source.typekit.com/source-han-serif/cn/)
GitHub address: [https://github.com/adobe-fonts/source-han-sans/tree/release/OTF/SimplifiedChinese](https://github.com/adobe-fonts/source-han-sans/tree/release/OTF/SimplifiedChinese)
After opening the link, you can select one from there:
!(#)
You can also download it from a cloud drive: [https://pan.baidu.com/s/10-w1JbXZSnx3Tm6uGpPGOw](https://pan.baidu.com/s/10-w1JbXZSnx3Tm6uGpPGOw), extraction code: yxqu.
You can download an OTF font, such as SourceHanSansSC-Bold.otf, and place this file in the directory where your code is executed:
Place the SourceHanSansSC-Bold.otf file in the directory where your code is executed:
## Example
```python
import numpy as np
from matplotlib import pyplot as plt
import matplotlib
zhfont1 = matplotlib.font_manager.FontProperties(fname="SourceHanSansSC-Bold.otf")
x = np.arange(1,11)
y = 2 * x + 5
plt.title(" - Test", fontproperties=zhfont1)
plt.xlabel("x Axis", fontproperties=zhfont1)
plt.ylabel("y Axis", fontproperties=zhfont1)
plt.plot(x,y)
plt.show()
The output is as follows:
!(#)
> Additionally, we can use system fonts:
>
> ```python
> from matplotlib import pyplot as plt
> import matplotlib
>
> a=sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])
> for i in a:
> print(i)
> ```
> This prints all registered font names from your font_manager's ttflist. Find a Chinese font you like, for example: STFangsong (FangSong), Heiti TC (HeiTi), then add the following code:
>
> ```python
> plt.rcParams['font.family']=['STFangsong']
> ```
We can also customize the font style:
## Example
```python
import numpy as np
from matplotlib import pyplot as plt
import matplotlib
# fname is the path to your downloaded font file. Note the path to SourceHanSansSC-Bold.otf. The size parameter sets the font size.
zhfont1 = matplotlib.font_manager.FontProperties(fname="SourceHanSansSC-Bold.otf", size=18)
font1 ={'color':'blue','size':20}
font2 ={'color':'darkred','size':15}
x = np.arange(1,11)
y =2* x + 5
# fontdict can use CSS to set font style
plt.title(" - Test", fontproperties=zhfont1, fontdict = font1)
# fontproperties sets Chinese display, fontsize sets font size
plt.xlabel("x Axis", fontproperties=zhfont1)
plt.ylabel("y Axis", fontproperties=zhfont1)
plt.plot(x,y)
plt.show()
The output is as follows:
!(#)
### Positioning the Title and Labels
The **title()** method provides a `loc` parameter to set the position of the title. It can be set to: **'left', 'right', and 'center'**, with the default being **'center'**.
The **xlabel()** method provides a `loc` parameter to set the position of the x-axis label. It can be set to: **'left', 'right', and 'center'**, with the default being **'center'**.
The **ylabel()** method provides a `loc` parameter to set the position of the y-axis label. It can be set to: **'bottom', 'top', and 'center'**, with the default being **'center'**.
## Example
```python
import numpy as np
from matplotlib import pyplot as plt
import matplotlib
# fname is the path to your downloaded font file. Note the path to SourceHanSansSC-Bold.otf. The size parameter sets the font size.
zhfont1 = matplotlib.font_manager.FontProperties(fname="SourceHanSansSC-Bold.otf", size=18)
font1 ={'color':'blue','size':20}
font2 ={'color':'darkred','size':15}
x = np.arange(1,11)
y =2* x + 5
# fontdict can use CSS to set font style
plt.title(" - Test", fontproperties=zhfont1, fontdict = font1, loc="left")
# fontproperties sets Chinese display, fontsize sets font size
plt.xlabel("x Axis", fontproperties=zhfont1, loc="left")
plt.ylabel("y Axis", fontproperties=zhfont1, loc="top")
plt.plot(x,y)
plt.show()
The output is as follows:
!(#)
YouTip