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時,繪圖的透明度就會丟失。您可以觀察以上圖片中的差異。

廣告