Keywords: Matplotlib | Figure Size | TIFF Format | Data Visualization | Python Plotting
Abstract: This article provides a comprehensive exploration of precise figure size and format control in Matplotlib. By analyzing core Q&A data, it focuses on the correct timing and parameter configuration of the plt.figure(figsize=()) method for setting figure dimensions, while deeply examining TIFF format support. The article also supplements with size conversion methods between different units (inches, centimeters, pixels), offering complete code examples and best practice recommendations to help readers master professional data visualization output techniques.
Core Methods for Figure Size Control
In Matplotlib, precise control of figure dimensions is crucial for achieving high-quality data visualization. According to the best answer in the Q&A data, the most direct and effective method is using the plt.figure(figsize=(width, height)) function, where size parameters are specified in inches. This method must be executed before calling any plotting functions to ensure the size settings are correctly applied to the entire figure.
Code Implementation and Execution Order
Based on the code from the original question, the corrected complete implementation should follow a specific execution sequence:
import matplotlib.pyplot as plt
# First set the figure size
plt.figure(figsize=(4, 3))
# Define data
list1 = [3, 4, 5, 6, 9, 12]
list2 = [8, 12, 14, 15, 17, 20]
# Create the plot
plt.plot(list1, list2)
# Save in specified format
plt.savefig('fig1.tiff', dpi=300)
plt.close()
TIFF Format Support and Considerations
Regarding TIFF format support, as mentioned in Answer 1, Matplotlib's backend support may vary depending on the installation environment and configuration. In most standard installations, Matplotlib can handle TIFF format, but verification before actual use is recommended. If TIFF support issues are encountered, consider using more widely supported formats like PNG or PDF as alternatives.
Alternative Size Setting Methods
In addition to directly using the plt.figure() method, as mentioned in Answer 2, default figure sizes can also be set by modifying global parameters:
plt.rcParams["figure.figsize"] = [4, 3]
This approach is suitable for scenarios requiring consistent figure sizes throughout a session, though it offers slightly less flexibility compared to direct setting.
Size Conversion Between Different Units
The reference article provides practical methods for size conversion between different units. Although Matplotlib natively uses inches as the size unit, simple mathematical conversions allow convenient use of centimeter or pixel units:
Centimeters to Inches
cm = 1/2.54 # Conversion factor from centimeters to inches
plt.figure(figsize=(10*cm, 7.5*cm)) # 10cm × 7.5cm
Pixels to Inches
px = 1/plt.rcParams['figure.dpi'] # Conversion factor from pixels to inches
plt.figure(figsize=(800*px, 600*px)) # 800px × 600px
DPI Settings and Output Quality
Setting the DPI (dots per inch) parameter in the savefig function is crucial for output quality. Higher DPI values (such as 300) produce sharper images suitable for printing and publication, but correspondingly increase file size. For screen display, 150-200 DPI is typically sufficient.
Best Practices Summary
Integrating content from both Q&A data and reference articles, the following best practices can be summarized: First, clarify figure size requirements and choose appropriate setting methods; second, pay attention to function call order to ensure size settings precede plotting; finally, select suitable file formats and DPI settings based on output purposes. The combined use of these steps ensures the generation of visualization graphics that meet professional standards.