如何在 Matplotlib 中使轴透明?
matplotlibpythondata visualization
要在 matplotlib 中使轴透明,我们可以采取以下步骤,
设置图形大小并调整子图之间和周围的填充。
使用 figure() 方法创建新图形或激活现有图形。
将 '~.axes.Axes' 添加到图形作为子图布置的一部分。
设置当前轴的表面颜色。
向图形添加轴。
使用 numpy 创建 t 和 s 数据。
绘制 t 和s 个数据点,使用 plot() 方法在轴 2 上绘制(来自步骤 5)。
要使轴透明,请使用 set_alpha() 方法并保持 alpha 值最小。
要显示图形,请使用 show() 方法。
示例
import matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax1 = fig.add_subplot(1, 1, 1) ax1.set_facecolor('orange') ax2 = fig.add_axes([0.5, 0.5, 0.3, 0.3]) t = np.arange(0, 1, 0.01) s = np.sin(t) ax2.plot(t, s, linewidth=2) ax2.patch.set_alpha(0.01) plt.show()