📜  如何使用Python在绘图中添加标签?

📅  最后修改于: 2022-05-13 01:55:31.874000             🧑  作者: Mango

如何使用Python在绘图中添加标签?

先决条件: Python Matplotlib

在本文中,我们将讨论使用Python的Matplotlib 向绘图添加标签。但首先,了解什么是情节中的标签。写在垂直轴(比如 Y 轴)和水平轴(比如 X 轴)上的标题或副标题提高了对绘制统计数据的理解质量。

示例:让我们创建一个简单的绘图

Python
# python program to plot graph without labels
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
  
  
# it will take x coordinates by default 
# starting from 0,1,2,3,4...
y = np.array([3, 8, 1, 10])
  
plt.plot(y)
plt.show()


Python
# python program for plots with label
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
  
  
# Number of chidern it was default in earlier case
x = np.array([0, 1, 2, 3])
  
# Number of families
y = np.array([3, 8, 1, 10])
  
plt.plot(x, y)
  
# Label for x-axis
plt.xlabel("Number of Childerns")
  
# Label for y-axis
plt.ylabel("Number of Families")
  
plt.show()  # for display


Python3
# python program for plots with label
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
  
  
# Number of chidern it was default in earlier case
x = np.array([0, 1, 2, 3])
  
# Number of families
y = np.array([3, 8, 1, 10])
  
plt.plot(x, y)
  
# Label for x-axis
plt.xlabel("Number of Childerns")
  
# Label for y-axis
plt.ylabel("Number of Families")
  
# title of the plot
plt.title("Survey Of Colony")
  
plt.show()  # for display


Python
# Adding font properties to labels and titles
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
  
  
# Number of Childerns
x = np.array([0, 1, 2, 3])
  
# Number of Families
y = np.array([3, 8, 1, 10])
  
# label including this form1 will have these properties
form1 = {'family': 'serif', 'color': 'blue', 'size': 20}
  
# label including this form2 will have these properties
form2 = {'family': 'serif', 'color': 'darkred', 'size': 15}
  
plt.plot(x, y)
plt.xlabel("Number of Childerns", fontdict=form1)
plt.ylabel("Number of Families", fontdict=form1)
plt.title("Survey Of Colony", fontdict=form2)
plt.show()


输出:



没有标签或标题的绘图

为绘图创建标签

通过使用库的 pyplot()函数,我们可以添加 xlabel() 和 ylabel() 来设置 x 和 y 标签。

示例:让我们在上面的图中添加标签

Python

# python program for plots with label
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
  
  
# Number of chidern it was default in earlier case
x = np.array([0, 1, 2, 3])
  
# Number of families
y = np.array([3, 8, 1, 10])
  
plt.plot(x, y)
  
# Label for x-axis
plt.xlabel("Number of Childerns")
  
# Label for y-axis
plt.ylabel("Number of Families")
  
plt.show()  # for display

输出:

用标签绘图

如果您想让它更易于理解,只需添加一行代码即可为绘图添加标题

plt.title("Survey Of Colony")

例子:

蟒蛇3

# python program for plots with label
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
  
  
# Number of chidern it was default in earlier case
x = np.array([0, 1, 2, 3])
  
# Number of families
y = np.array([3, 8, 1, 10])
  
plt.plot(x, y)
  
# Label for x-axis
plt.xlabel("Number of Childerns")
  
# Label for y-axis
plt.ylabel("Number of Families")
  
# title of the plot
plt.title("Survey Of Colony")
  
plt.show()  # for display

输出:

带标题的绘图

设置标题和标签的字体属性

为了使绘图更具吸引力,请使用 xlabel()、ylabel() 和 title() 中的 fontdict 参数来应用字体属性。

Python

# Adding font properties to labels and titles
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
  
  
# Number of Childerns
x = np.array([0, 1, 2, 3])
  
# Number of Families
y = np.array([3, 8, 1, 10])
  
# label including this form1 will have these properties
form1 = {'family': 'serif', 'color': 'blue', 'size': 20}
  
# label including this form2 will have these properties
form2 = {'family': 'serif', 'color': 'darkred', 'size': 15}
  
plt.plot(x, y)
plt.xlabel("Number of Childerns", fontdict=form1)
plt.ylabel("Number of Families", fontdict=form1)
plt.title("Survey Of Colony", fontdict=form2)
plt.show()

输出:

使用字体属性绘图