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
Plotting at full resolution with matplotlib.pyplot, imshow() and savefig()
To plot at full resolution with matplotlib.pyplot, imshow() and savefig(), we can set the DPI (dots per inch) value between 600 to 1200 for high-quality output. This is essential when creating publication-ready images or detailed visualizations.
Key Parameters for High Resolution
The main parameters that control image quality are:
- dpi − Dots per inch, controls resolution (600-1200 for high quality)
- bbox_inches='tight' − Removes extra whitespace
- pad_inches − Controls padding around the figure
Basic High-Resolution Example
Here's how to create and save a high-resolution image using imshow() ?
import matplotlib.pyplot as plt
import numpy as np
# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create sample data
data = np.random.rand(5, 5)
# Display the data as an image
plt.imshow(data, cmap="plasma")
plt.colorbar()
plt.title("High Resolution Image")
# Save with high DPI
plt.savefig("myimage.png", dpi=1200, bbox_inches='tight')
plt.show()
Advanced Resolution Control
For more control over the output quality, you can specify additional parameters ?
import matplotlib.pyplot as plt
import numpy as np
# Create a larger dataset for better visualization
data = np.random.rand(20, 20)
# Create figure with specific size
fig, ax = plt.subplots(figsize=(8, 6))
# Display image with interpolation
im = ax.imshow(data, cmap="viridis", interpolation="bilinear")
# Add colorbar and labels
plt.colorbar(im)
ax.set_title("High Resolution Heatmap", fontsize=14)
ax.set_xlabel("X-axis", fontsize=12)
ax.set_ylabel("Y-axis", fontsize=12)
# Save with multiple format options
plt.savefig("heatmap.png", dpi=1200, bbox_inches='tight', pad_inches=0.1)
plt.savefig("heatmap.pdf", dpi=1200, bbox_inches='tight') # Vector format
plt.show()
Comparison of DPI Settings
| DPI Value | Quality | File Size | Use Case |
|---|---|---|---|
| 150 | Screen viewing | Small | Web display |
| 300 | Print quality | Medium | Standard printing |
| 600-1200 | High resolution | Large | Publication/Professional |
Output
Conclusion
Use DPI values of 600-1200 with savefig() for high-resolution images. Combine with bbox_inches='tight' to remove extra whitespace and choose appropriate file formats (PNG for raster, PDF/SVG for vector graphics).
Advertisements
