Matplotlib - PostScript



PostScript is a page description language and a dynamically typed, stack-based programming language often abbreviated as PS. Created by Adobe Systems in the early 1980s, its primary purpose is to describe the layout and graphics of printed pages. It is widely used in electronic publishing and desktop publishing applications.

A PostScript file can be printed or displayed on different devices without losing quality. This is the key advantage when generating plots for different purposes.

PostScript in Matplotlib

In the context of Matplotlib, PostScript serves as a backend rendering engine (used for displaying figures on the screen or writing to files), enabling users to generate publication-quality plots and graphics. When you choose the PostScript backend, Matplotlib generates PostScript code to describe the layout and appearance of the plot.

The PostScript code generated by Matplotlib includes instructions for drawing lines, shapes, text, and other graphical elements on a page. These instructions are written in the PostScript programming language.

The Matplotlib PostScript Backend (matplotlib.backends.backend_ps) can produce both .ps and .eps files.

Create a PostScript file

Let's explore how to create a simple plot and save it as a PostScript file using Matplotlib.

Example 1

This example demonstrates how to create a simple plot with wrapped text and saves it as a PostScript file(.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...')

Output

If you visit the folder where the Output is saved you can observe resultant PostScript file named test.ps.

Successfully created the PostScript (PS) file...

Example 2

Here is another example that demonstrates how to use the PostScript backend to generate a plot and save it to an encapsulated PostScript (EPS) file.

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...')

Output

If you visit the folder where the Output is saved you can observe resultant PostScript file named example.eps.

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

Customizing PostScript Output

Adjusting the PostScript output settings in Matplotlib allows you to enhance the visual quality of Encapsulated PostScript (EPS) files. By default, Matplotlib uses a distillation process when creating EPS files. This distillation step removes specific PostScript operators that LaTeX considers illegal in an EPS file.

One effective workaround involves modifying the resolution parameter to achieve better visual results. The rcParams["ps.distiller.res"] parameter controls the resolution of the EPS files, with the default value set to 6000. Increasing this value can result in larger files but may significantly improve visual quality and maintain reasonable scalability.

Example 1

This example demonstrates how adjusting resolution parameter can enhance the visual quality of EPS files.

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

If you visit the folder where the Output is saved you can observe resultant PostScript file named Output customized file.eps.

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

Example 2

Here is an example that demonstrates how the transparency is preserved when saving the plot as an .eps file by setting the transparent=True parameter in the savefig() function.

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()

Output

On executing the above code you will get the following output −

postscript_ex3

Whenever plots are saved in .eps/.ps, then the transparency of the plots get lost. You can observe the difference in th eabove image.

Advertisements