Python 图中上标
pythonserver side programmingprogrammingmatplotlib更新于 2023/12/27 19:58:00
要在 Python 中添加上标,我们可以采取以下步骤 −
使用 numpy 为 a 和 f 创建点。
使用 plot() 方法绘制 f = ma 曲线,标签为 f=ma。
为带有上标的图添加标题,即 kgms-2。
为带有上标的图添加 xlabel ,即 ms-2。
为带有上标的图添加 ylabel ,即 kg。
要放置图例,请使用legend() 方法。
要显示图形,请使用 show() 方法。
示例
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True a = np.linspace(1, 10, 100) m = 20 f = m*a plt.plot(a, f, c="red", lw=5, label="f=ma") plt.title("Force $\mathregular{kgms^{-2}}$") plt.xlabel("Acceleration $\mathregular{ms^{-2}}$") plt.ylabel("Acceleration $\mathregular{kg}$") plt.legend() plt.show()