如何在 Matplotlib 中以高分辨率绘制和保存图形
为了在 Matplotlib 以高分辨率保存图形,我们控制 savefig()
函数的各种参数。同样,我们可以通过在 figure()
函数中设置 dpi
参数的高值来绘制高分辨率的图形。
Matplotlib 中以高分辨率绘制图形
我们可以通过在 matplotlib.pyplot.figure()
函数中设置较高的 dpi
值来绘制高分辨率的图形。
matplotlib.pyplot.figure()
的语法:
matplotlib.pyplot.figure(num=None,
figsize=None,
dpi=None,
facecolor=None,
edgecolor=None,
frameon=True,
FigureClass=<class 'matplotlib.figure.Figure'>,
**kwargs)
dpi 代表每英寸点数。它代表图中每英寸的像素数。matplotlib.pyplot.figure()
函数中 dpi 的默认值为 100。我们可以设置更高的 dpi 值来生成高分辨率图。但是,增加 dpi 也会放大数字,我们必须调整 dpi 的适当值,以免数字被裁剪。
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 20)
m = 1.5
c = 2
y = m*x + c
plt.figure(dpi=150)
plt.plot(x, y)
plt.title("y=mx+c")
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.show()
输出:
Matplotlib 以高分辨率保存图形
我们可以通过在 matplotlib.pyplot.savefig()
函数中设置较高的 dpi 值来绘制高分辨率的图形。
matplotlib.pyplot.savefig()
的语法:
matplotlib.pyplot.savefig(fname,
dpi=None,
facecolor='w',
edgecolor='w',
orientation='portrait',
papertype=None,
format=None,
transparent=False,
bbox_inches=None,
pad_inches=0.1,
frameon=None,
metadata=None)
我们可以通过 savefig()
函数中的 dpi
参数来控制所保存图形的分辨率。同样,我们也可以在保存图时更改格式。通常,对于高分辨率图,png
比 jpeg
更好,因为 png
是一种无损压缩格式,而另一种是有损压缩格式。
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 5)
m = 1.5
c = 2
y = m*x + c
plt.plot(x, y)
plt.title("y=mx+c")
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.savefig("High resoltion.png",dpi=300)
这样可以在当前工作目录中以比默认情况更高的分辨率将图另存为 High resoltion.png
。
相关文章
Pandas DataFrame DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
Pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:Python
-
Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。
Pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:Python
-
本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。
Pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:Python
-
本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串