首页 > 解决方案 > Matplotlib 绘图在另存为 pdf 时失去透明度

问题描述

pdf以matplotlib 格式保存绘图时,不会保留透明度。我已经尝试过,在svg这里它确实如此,因此它能够以透明度保存图形。

我已经看到Matplotlib中的解决方案等解决方案在保存为 .pdf 时会失去透明度,但将轴设置为光栅化对我不起作用。这是 matplotlib 中的错误还是我做错了什么?

注意:我使用的是 python 3.9.2

代码

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2*np.pi)
y = np.cos(x)

fig, ax = plt.subplots(figsize=(10,6))
ax.plot(x, y, '--')

fig.savefig('example.pdf', transparent=True, bbox_inches='tight')
fig.savefig('example.svg', transparent=True, bbox_inches='tight')

标签: python-3.xmatplotlibpdfsvgtransparency

解决方案


推荐阅读