为数据轴添加标签
一个完整的图,除去线条和图例外,还需要为坐标轴添加标签,用来解释坐标轴表示的含义。一般而言x轴的标签在x轴的下面,居中放置,y轴的标签在y轴的左面,居中放置。标签要求简练,有单位的要标明单位。
代码展示
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
abs = np.random.randn(100)
y1 = 2 * x + 9 + abs
y2 = 2 * x + 9
plt.scatter(x, y1, c="red", label="scatter")
plt.plot(x, y2, ls="-", lw=1, label="line")
plt.legend()
plt.xlabel("x")
plt.ylabel("y")
plt.show()
重点代码解释
plt.xlabel(string)
plt.ylabel(string)
- string:标签文本内容