Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How do you improve Matplotlib image quality?
To improve Matplotlib image quality, you can increase the DPI (dots per inch) value and use vector formats like PDF or EPS. Higher DPI values (600+) produce sharper images, while vector formats maintain quality at any zoom level.
Key Methods for Better Image Quality
There are several approaches to enhance your Matplotlib output ?
- Increase DPI: Use values like 300, 600, or 1200 for high-resolution output
- Vector formats: Save as PDF, EPS, or SVG for scalable quality
- Figure size: Set appropriate dimensions before plotting
- Font settings: Adjust font sizes and families for clarity
Example: High-Quality Heatmap
Here's how to create and save a high-quality image with proper DPI settings ?
import numpy as np
import matplotlib.pyplot as plt
# Set figure size and layout
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create sample data for heatmap
data = np.array([
[0.1, 0.7, 0.6, 0.3],
[0.2, 0.6, 0.5, 0.2],
[0.8, 0.3, 0.80, 0.01],
[0.3, 0.4, 0.2, 0.1]
])
# Create heatmap with high quality settings
plt.imshow(data, interpolation="nearest", cmap="RdYlGn_r")
plt.colorbar()
plt.title("High-Quality Heatmap")
# Save with high DPI for print quality
plt.savefig("high_quality_image.eps", dpi=1200, bbox_inches='tight')
plt.show()
DPI and Format Comparison
| DPI Value | Quality Level | Best Use Case |
|---|---|---|
| 72-96 | Screen display | Web, presentations |
| 300 | Print quality | Publications, reports |
| 600-1200 | High resolution | Professional printing |
Additional Quality Settings
For even better results, combine multiple quality enhancement techniques ?
import matplotlib.pyplot as plt
import numpy as np
# Enhanced quality settings
plt.rcParams['figure.dpi'] = 150 # Display DPI
plt.rcParams['savefig.dpi'] = 300 # Save DPI
plt.rcParams['font.size'] = 12
plt.rcParams['axes.linewidth'] = 1.2
# Sample plot
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.figure(figsize=(8, 5))
plt.plot(x, y, linewidth=2, label='sin(x)')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('High-Quality Line Plot')
plt.legend()
plt.grid(True, alpha=0.3)
# Save with multiple formats
plt.savefig('quality_plot.pdf', dpi=300, bbox_inches='tight')
plt.savefig('quality_plot.png', dpi=300, bbox_inches='tight')
plt.show()
Conclusion
Use DPI values of 300+ for print quality and vector formats (PDF, EPS, SVG) for scalable images. Combine proper figure sizing with high DPI settings for the best results in professional publications.
Advertisements
