python Set the font size and font type of xlabel and ylabel axes

  • 2021-07-24 11:31:29
  • OfStack

This article introduces python setting xlabel, ylabel coordinate axis font size, font type, to share with you, as follows:


#--coding:utf-8--
import matplotlib.pyplot as plt
 
# Data setting 
x1 =[0,5000,10000, 15000, 20000, 25000, 30000, 35000, 40000, 45000, 50000, 55000];
y1=[0, 223, 488, 673, 870, 1027, 1193, 1407, 1609, 1791, 2113, 2388];
 
x2 =[0,5000,10000, 15000, 20000, 25000, 30000, 35000, 40000, 45000, 50000, 55000];
y2=[0, 214, 445, 627, 800, 956, 1090, 1281, 1489, 1625, 1896, 2151];
 
# Set the output picture size 
figsize = 11,9
figure, ax = plt.subplots(figsize=figsize)
 
# In the same 1 Draw two broken lines on a picture 
A,=plt.plot(x1,y1,'-r',label='A',linewidth=5.0)
B,=plt.plot(x2,y2,'b-.',label='B',linewidth=5.0)
 
# Set the legend and set the font and size of the legend 
font1 = {'family' : 'Times New Roman',
'weight' : 'normal',
'size' : 23,
}
legend = plt.legend(handles=[A,B],prop=font1)
 
# Set the size of the coordinate scale value and the font of the scale value 
plt.tick_params(labelsize=23)
labels = ax.get_xticklabels() + ax.get_yticklabels()
[label.set_fontname('Times New Roman') for label in labels]
 
# Set the name of the ordinate and the corresponding font format 
font2 = {'family' : 'Times New Roman',
'weight' : 'normal',
'size' : 30,
}
plt.xlabel('round',font2)
plt.ylabel('value',font2)
 
# Save the file to the file and draw a picture 
plt.savefig('figure.eps')
plt.show()

Example 1: Setting Axis Title for 2-Dimensional Subgraph


#!/usr/bin/python3
#code-python(3.6)
import matplotlib.pyplot as plt
fig = plt.figure() # Set canvas 
# Divide the canvas into 2 Row 2 Column, total 4 Child diagram, and position it in the first 1 Subgraph 
ax = fig.add_subplot(2,2,1)  # Return to the 1 Subgraph 
ax.set_xlabel('Month') # Set horizontal axis title for subgraph 
ax.set_ylabel('Year') # Set vertical axis title for subgraph 
plt.show()

Function description


# The values of parameters in the function are the default parameter values 
matplotlib.axes.Axes.set_xlabel(xlabel, fontdict=None, labelpad=None, **kwargs)

Example 2: Setting Axis Title for 3D Subgraph


#!/usr/bin/python3
#code-python(3.6)
import matplotlib.pyplot as plt
fig = plt.figure() # Set canvas 
from mpl_toolkits.mplot3d import Axes3D
# Divide the canvas into 2 Row 1 Column, total 2 Child diagram, and position it in the first 2 Subgraph 
ax = fig.add_subplot(212, projection='3d')
ax.set_xlabel('Month') # Set for a subgraph x Axis title 
ax.set_ylabel('Year') # Set for a subgraph y Axis title 
ax.set_zlabel('Sales') # Set for a subgraph z Axis title 
plt.show()

Related articles: