我正在努力从没有背景和边框的 matplotlib 中保存情节。 特别是我想以两种格式导出:
- svg,只有绘图线(没有背景,没有轴,没有框架,没有边框)
- png,背景透明,边界框与绘图完全吻合。
更准确的说,我举个例子。
i = 1000
j = 1024
periods = 6
x = np.array([np.linspace(0, (2 * (periods) * np.pi), i)]).T
x = np.repeat(x, j, axis=1)
n = (1 * (np.random.normal(size=(j))) *
np.random.uniform(low=1, high=1, size=j))[:, np.newaxis]
n = np.repeat(n, i, axis=1).T
y = np.sin(x) * (np.sin(n)+4) + 0.5 * n**2
xout = np.array([np.linspace(0, 2 * (2 * (periods) * np.pi), 2 * i)]).T
xout = np.repeat(xout, j, axis=1)
yout = np.concatenate((y, y[::-1]))
f1 = plt.figure("GOOD1")
plt.axis('off')
plt.plot(x, y, 'b', alpha=(0.015625))
plt.savefig("GOOD1.svg", bbox_inches='tight', transparent=True)
plt.savefig("GOOD1.png", bbox_inches='tight', transparent=True)
这看起来与我想要导出的相似,但在 png 和 svg 图像的右侧仍然有一个小空间; svg 也有背景。
第二个问题是关于alpha channel 的。 我知道它被限制为 8 位,因此将 alpha 值降低得更多(至 0.005)将使情节消失。 引用前面的代码,有没有办法在不丢失绘图的情况下绘制更多的线条并提高透明度?
最佳答案
我不确定如何回答您问题的第二部分。不过,我相信这段代码会处理边框和背景问题。
import numpy as np
import matplotlib.pyplot as plt
i = 1000
j = 1024
periods = 6
x = np.array([np.linspace(0, (2 * (periods) * np.pi), i)]).T
x = np.repeat(x, j, axis=1)
n = (1 * (np.random.normal(size=(j))) *
np.random.uniform(low=1, high=1, size=j))[:, np.newaxis]
n = np.repeat(n, i, axis=1).T
y = np.sin(x) * (np.sin(n)+4) + 0.5 * n**2
xout = np.array([np.linspace(0, 2 * (2 * (periods) * np.pi), 2 * i)]).T
xout = np.repeat(xout, j, axis=1)
yout = np.concatenate((y, y[::-1]))
f1 = plt.figure("GOOD1")
ax = f1.add_subplot(111)
ax.axis('off')
ax.set_position([0, 0, 1, 1])
ax.plot(x, y, 'b', alpha=(0.015625))
ax.set_xlim((x.min(), x.max()))
ax.set_ylim((y.min(), y.max()))
f1.patch.set_alpha(0.)
ax.patch.set_alpha(0.)
f1.savefig("GOOD1.svg", bbox_inches=0, transparent=True)
f1.savefig("GOOD1.png", bbox_inches=0, transparent=True)
关于python - matplotlib - 保存没有背景和边框的绘图线和透明度 8 位 alpha channel 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35346480/