在 Matplotlib 中绘制曲线以区分抗锯齿
matplotlibserver side programmingprogramming更新于 2025/8/28 15:52:17
要通过曲线区分抗锯齿,我们可以采取以下步骤 −
使用 subplot() 方法向当前图形添加子图,其中 nrows=1、ncols=2 和 index=1。
使用 plot() 方法绘制曲线,其中抗锯齿标志为 false,颜色为红色。
使用 legend() 方法将图例放置在左上角。
使用 subplot() 方法,其中 nrows=1、ncols=2 和 index=2。
使用 plot() 方法绘制曲线,其中抗锯齿标志为 true,颜色为绿色。
使用 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 plt.subplot(1, 2, 1) plt.plot(range(10), np.sin(range(10)), antialiased=False, label='Antialiasing Off', c="red") plt.legend(loc="upper left") plt.subplot(1, 2, 2) plt.plot(range(10), np.cos(range(10)), antialiased=True, label='Antialiasing On', c="green") plt.legend(loc="upper right") plt.show()
输出


