Matplotlib 简明教程
Matplotlib - Saving Figures
在 Matplotlib 库中保存图形允许我们将所绘制的图形输出为各种文件格式,例如 PNG、PDF、SVG 等,以便在各种报告、演示文稿或出版物中使用这些已保存的图形。 Matplotlib 库提供了 savefig() 函数用于保存我们创建的图形。
Saving figures in Matplotlib library allows us to export our plots to various file formats such as PNG, PDF, SVG and so on to use those saved plots in various reports, presentations or publications. Matplotlib library provides the savefig() function for to save the plot that we have created.
Common File Formats for Saving
-
PNG (.png) − Good for general-purpose images which supports transparency.
-
JPEG (.jpg) − Suitable for images with smooth gradients but may lose some quality due to compression.
-
PDF (.pdf) − Ideal for vector-based images scalable without loss of quality.
-
SVG (.svg) − Scalable Vector Graphics which suitable for web-based or vector-based graphics.
在 Matplotlib 库中保存图形对于以各种格式保存可视化非常有用,因为它可以确保可以根据需要在不同的上下文中共享、使用或嵌入它们。通过调整文件格式和分辨率,我们可以根据要求在图像质量和文件大小之间取得平衡。
Saving figures in Matplotlib library is useful for preserving visualizations in various formats by ensuring they can be shared, used or embedded in different contexts as needed. Adjusting the file format and resolution allows us to balance image quality and file size based on your requirements.
Syntax
以下是不使用 savefig() 方法的语法和参数。
The following is the syntax and parameters for using the savefig() method.
plt.savefig(fname, dpi=None, bbox_inches='tight', pad_inches=0.1, format=None, kwargs)
其中,
Where,
-
fname − The file name or path of the file to save the figure. The file extension determines the file format such as ".png", ".pdf".
-
dpi − Dots per inch i.e. resolution for the saved figure. Default is "None" which uses the Matplotlib default.
-
bbox_inches − Specifies which part of the figure to save. Options include 'tight', 'standard' or a specified bounding box in inches.
-
pad_inches − Padding around the figure when bbox_inches='tight'.
-
format − Explicitly specify the file format. If 'None' the format is inferred from the file extension in fname.
-
kwargs − Additional keyword arguments specific to the chosen file format.
Saving the plot in specified location
在此示例中,我们使用 plot() 函数创建一个简单的折线图,然后我们尝试使用指定的名称将绘制的图像保存在指定的位置。
In this example we are creating a simple line plot by using the plot() function and then we are trying to save the plotted image in the specified location with the specified filename.
import matplotlib.pyplot as plt
# Data
x = [22,1,7,2,21,11,14,5]
y = [24,2,12,5,5,5,9,12]
plt.plot(x,y)
# Customize the plot (optional)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Simple Line Plot')
# Display the plot
plt.savefig('matplotlib/Savefig/lineplot.png')
plt.show()
执行上述代码,我们将得到以下输出 −
On executing the above code we will get the following output −
Saving plot in .svg format
在此处,这是使用 savefig() 保存所绘制图形的另一个示例,它将文件格式指定为 svg,并将 dpi 指定为 300 来设置分辨率。
Here, this is another example of saving the plotted plot by using the savefig() by specifying the file format as svg and dpi as 300 to set the resolution.
import matplotlib.pyplot as plt
# Data
x = [22,1,7,2,21,11,14,5]
y = [24,2,12,5,5,5,9,12]
plt.plot(x,y)
# Customize the plot (optional)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Simple Line Plot')
# Display the plot
plt.savefig('matplotlib/Savefig/lineplot2.svg',dpi = 500)
plt.show()
执行上述代码,我们将得到以下输出 −
On executing the above code we will get the following output −
Note
-
We should call savefig() before calling show() if we want to save the figure with the exact appearance shown on the screen otherwise empty file will be saved.
-
The file extension in the fname parameter determines the format of the saved file. Matplotlib automatically infers the format if format is None.