Matplotlib - PostScript



PostScript 是一种页面描述语言和动态类型、基于堆栈的编程语言,通常缩写为 PS。它由Adobe Systems公司在20世纪80年代初创建,其主要目的是描述打印页面的布局和图形。它广泛用于电子出版和桌面出版应用程序。

PostScript文件可以在不同的设备上打印或显示而不会损失质量。这是生成用于不同目的的绘图的关键优势。

Matplotlib中的PostScript

在Matplotlib的上下文中,PostScript充当后端渲染引擎(用于在屏幕上显示图形或写入文件),使用户能够生成出版质量的绘图和图形。当您选择PostScript后端时,Matplotlib会生成PostScript代码来描述绘图的布局和外观。

Matplotlib生成的PostScript代码包括用于在页面上绘制线条、形状、文本和其他图形元素的指令。这些指令是用PostScript编程语言编写的。

Matplotlib PostScript后端(matplotlib.backends.backend_ps)可以生成.ps和.eps文件。

创建PostScript文件

让我们探讨如何使用Matplotlib创建一个简单的绘图并将其保存为PostScript文件。

示例1

此示例演示如何创建一个带有换行文本的简单绘图并将其保存为PostScript文件(.ps)。

import matplotlib
import matplotlib.pyplot as plt

import textwrap
from pylab import *

# Generate a string containing printable characters (ASCII 32 to 126)
text_to_wrap = "".join(c for c in map(chr, range(32, 127)) if c.isprintable())

# Wrap the string to fit within the figure
wrapped_text = "\n".join(textwrap.wrap(text_to_wrap))

# Add the wrapped text to the figure 
figtext(0, 0.5, wrapped_text)

# Save the figure to a PostScript file named "test.ps"
savefig("test.ps")
print('Successfully created the PostScript (PS) file...')

输出

如果您访问保存输出的文件夹,您可以看到名为test.ps的生成的PostScript文件。

Successfully created the PostScript (PS) file...

示例2

这是一个示例,演示如何使用PostScript后端生成绘图并将其保存到封装的PostScript (EPS) 文件。

import numpy as np
from matplotlib import pyplot as plt

# Generate data
x_data = np.linspace(1, 10, 100)
y_data = np.sin(x_data)

# Create the plot 
plt.plot(x_data, y_data, c='green', marker='o')
plt.grid()

# Save the figure to a PostScript file named "example.eps"
plt.savefig("example.eps")

print('Successfully created the encapsulated PostScript (EPS) file...')

输出

如果您访问保存输出的文件夹,您可以看到名为example.eps的生成的PostScript文件。

Successfully created the encapsulated PostScript (EPS) file...

自定义PostScript输出

调整Matplotlib中的PostScript输出设置允许您增强封装PostScript (EPS) 文件的视觉质量。默认情况下,Matplotlib在创建EPS文件时使用蒸馏过程。此蒸馏步骤会删除LaTeX认为在EPS文件中非法的特定PostScript操作符。

一种有效的解决方法包括修改分辨率参数以获得更好的视觉效果。rcParams["ps.distiller.res"]参数控制EPS文件的分辨率,默认值为6000。增加此值会导致文件更大,但可能会显著提高视觉质量并保持合理的缩放比例。

示例1

此示例演示如何调整分辨率参数可以增强EPS文件的视觉质量。

import numpy as np
import matplotlib.pyplot as plt

# Set the resolution for EPS files
plt.rcParams["ps.distiller.res"] = 12000

# Set the figure size and enable autolayout
plt.rcParams["figure.figsize"] = [7, 3.50]
plt.rcParams["figure.autolayout"] = True

# Generate data
x_data = np.linspace(1, 10, 100)
y_data = np.sin(x_data)

# Plotting
plt.plot(x_data, y_data, label='Sine Wave', color='green')

# Save the figure as an EPS file
plt.savefig('Output customized file.eps', format='eps', bbox_inches='tight')
print('Successfully created the output customized PostScript (EPS) file...')

输出

如果您访问保存输出的文件夹,您可以看到名为Output customized file.eps的生成的PostScript文件。

Successfully created the output customized PostScript (EPS) file...

示例2

这是一个示例,演示了通过在savefig()函数中设置transparent=True参数如何在将绘图保存为.eps文件时保留透明度。

import numpy as np
import matplotlib.pyplot as plt

# Adjust figure size and autolayout
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Generate data
x_data = np.linspace(1, 10, 100)
y_data = np.sin(x_data)

# Plot data with transparency
plt.plot(x_data, y_data, c='green', marker='o', alpha=.35, ms=10, lw=1)
plt.grid()

# Save plot as .eps by preserving the transparency
plt.savefig("lost_transparency_img.eps", transparent=True)  

# Display plot
plt.show()

输出

执行上述代码后,您将获得以下输出:

postscript_ex3

每当绘图保存为.eps/.ps时,绘图的透明度就会丢失。您可以观察以上图片中的差异。

广告
© . All rights reserved.