python - 如何在 matplotlib 中使轴透明?

标签 python matplotlib plot

所以我有以下示例绘制图形和插图:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()

ax1 = fig.add_subplot(1, 1, 1)
x = np.arange(100).reshape((10, 10))
ax1.imshow(x)

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)

这会产生下图: enter image description here

我想要的是插图有点透明(比如 alpha=0.5)。我想要一些类似于他们在 matplotlib 文档中的图例文档中所做的事情:

enter image description here

http://matplotlib.org/users/recipes.html#transparent-fancy-legends

这可能吗?有谁知道怎么做?

祝一切顺利

PD正如评论中提到的,这个问题的答案可以从链接问题的答案中得出。它在概念上略有不同(图形与轴),并且在这里 IMO 更直接。

最佳答案

好吧,玩了一会儿我发现这可以通过控制 patch 属性或轴来实现。

在上面的代码中添加以下代码会产生所需的结果:

ax2.patch.set_alpha(0.5)

enter image description here

这是有效的,因为补丁只是代表背景的图形(matplotlib 术语中的艺术家),正如我从文档中理解的那样:

Documentation

关于python - 如何在 matplotlib 中使轴透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33297954/

相关文章:

python - luigi 目标不存在的表

python - 如何在 matplotlib 中按列而不是行迭代子图

python - 如何在 Matplotlib 中连接图形坐标?

python - 如何解决蛇壁传送的bug

python - Elasticsearch没有显示任何匹配。 Python查询是正确的

python - Google Collab Notebook与普通的Python脚本创建了不同类型的绘图大小

python - python中的圆形/极坐标直方图

matlab - MATLAB 中的抗锯齿线和标记

c# - WPF 中的图形绘制(功能)

python - Bazel:如何在测试前运行 py_binary 生成输入文件