- Matplotlib Basics
- Matplotlib - Home
- Matplotlib - Introduction
- Matplotlib - Vs Seaborn
- Matplotlib - Environment Setup
- Matplotlib - Anaconda distribution
- Matplotlib - Jupyter Notebook
- Matplotlib - Pyplot API
- Matplotlib - Simple Plot
- Matplotlib - Saving Figures
- Matplotlib - Markers
- Matplotlib - Figures
- Matplotlib - Styles
- Matplotlib - Legends
- Matplotlib - Colors
- Matplotlib - Colormaps
- Matplotlib - Colormap Normalization
- Matplotlib - Choosing Colormaps
- Matplotlib - Colorbars
- Matplotlib - Text
- Matplotlib - Text properties
- Matplotlib - Subplot Titles
- Matplotlib - Images
- Matplotlib - Image Masking
- Matplotlib - Annotations
- Matplotlib - Arrows
- Matplotlib - Fonts
- Matplotlib - What are Fonts?
- Setting Font Properties Globally
- Matplotlib - Font Indexing
- Matplotlib - Font Properties
- Matplotlib - Scales
- Matplotlib - Linear and Logarthmic Scales
- Matplotlib - Symmetrical Logarithmic and Logit Scales
- Matplotlib - LaTeX
- Matplotlib - What is LaTeX?
- Matplotlib - LaTeX for Mathematical Expressions
- Matplotlib - LaTeX Text Formatting in Annotations
- Matplotlib - PostScript
- Enabling LaTex Rendering in Annotations
- Matplotlib - Mathematical Expressions
- Matplotlib - Animations
- Matplotlib - Artists
- Matplotlib - Styling with Cycler
- Matplotlib - Paths
- Matplotlib - Path Effects
- Matplotlib - Transforms
- Matplotlib - Ticks and Tick Labels
- Matplotlib - Radian Ticks
- Matplotlib - Dateticks
- Matplotlib - Tick Formatters
- Matplotlib - Tick Locators
- Matplotlib - Basic Units
- Matplotlib - Autoscaling
- Matplotlib - Reverse Axes
- Matplotlib - Logarithmic Axes
- Matplotlib - Symlog
- Matplotlib - Unit Handling
- Matplotlib - Ellipse with Units
- Matplotlib - Spines
- Matplotlib - Axis Ranges
- Matplotlib - Axis Scales
- Matplotlib - Axis Ticks
- Matplotlib - Formatting Axes
- Matplotlib - Axes Class
- Matplotlib - Twin Axes
- Matplotlib - Figure Class
- Matplotlib - Multiplots
- Matplotlib - Grids
- Matplotlib - Object-oriented Interface
- Matplotlib - PyLab module
- Matplotlib - Subplots() Function
- Matplotlib - Subplot2grid() Function
- Matplotlib - Anchored Artists
- Matplotlib - Manual Contour
- Matplotlib - Coords Report
- Matplotlib - AGG filter
- Matplotlib - Ribbon Box
- Matplotlib - Fill Spiral
- Matplotlib - Findobj Demo
- Matplotlib - Hyperlinks
- Matplotlib - Image Thumbnail
- Matplotlib - Plotting with Keywords
- Matplotlib - Create Logo
- Matplotlib - Multipage PDF
- Matplotlib - Multiprocessing
- Matplotlib - Print Stdout
- Matplotlib - Compound Path
- Matplotlib - Sankey Class
- Matplotlib - MRI with EEG
- Matplotlib - Stylesheets
- Matplotlib - Background Colors
- Matplotlib - Basemap
- Matplotlib Event Handling
- Matplotlib - Event Handling
- Matplotlib - Close Event
- Matplotlib - Mouse Move
- Matplotlib - Click Events
- Matplotlib - Scroll Event
- Matplotlib - Keypress Event
- Matplotlib - Pick Event
- Matplotlib - Looking Glass
- Matplotlib - Path Editor
- Matplotlib - Poly Editor
- Matplotlib - Timers
- Matplotlib - Viewlims
- Matplotlib - Zoom Window
- Matplotlib Plotting
- Matplotlib - Bar Graphs
- Matplotlib - Histogram
- Matplotlib - Pie Chart
- Matplotlib - Scatter Plot
- Matplotlib - Box Plot
- Matplotlib - Violin Plot
- Matplotlib - Contour Plot
- Matplotlib - 3D Plotting
- Matplotlib - 3D Contours
- Matplotlib - 3D Wireframe Plot
- Matplotlib - 3D Surface Plot
- Matplotlib - Quiver Plot
- Matplotlib Useful Resources
- Matplotlib - Quick Guide
- Matplotlib - Useful Resources
- Matplotlib - Discussion
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 −
Whenever plots are saved in .eps/.ps, then the transparency of the plots get lost. You can observe the difference in th eabove image.