matplotlib - 保存没有背景和边框的绘图线和透明度 8 位 alpha 通道问题

matplotlib - save plot lines without background and borders and transparency 8 bits alpha channel issue

我正在努力从没有背景和边框的 matplotlib 中保存绘图。 特别是我想以两种格式导出:

更准确的说,我举个例子

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 图像的右侧仍然有一个小的 space; svg 也有背景。

第二个问题是关于alpha通道的。 我知道它被限制为 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)