如何在 Matplotlib 中管理圖形的影像解析度


圖片包含一個由影像每英寸點 (DPI) 定義的 2-D 矩陣 RGB 資料點。影像的解析度很重要,因為高解析度影像的清晰度會更高。

Matplotlib 中有一個方法“plt.savefig()”,可根據畫素來確定影像大小。理想情況下它有一個“dpi”引數。

我們來看看如何在 Matplotlib 中管理圖形解析度。

示例

import matplotlib.pyplot as plt
import numpy as np

#Prepare the data for histogram
np.random.seed(1961)
nd = np.random.normal(13, 5, 1000)

#Define the size of the plot
plt.figure(figsize=(8,6))
plt.hist(nd)
plt.grid()

#set the dpi value to 300
plt.savefig('histogram_img.png', dpi=300)
plt.show()
plt.figure(figsize=(18,12))
plt.hist(nd)
plt.grid()

#Set the dpi value to 150
plt.savefig('histogram_100.png', dpi=150)
plt.show()

執行上述程式碼後,輸出內容如下:

輸出

於: 23-2021 年 2 月更新

1K+ 瀏覽量

開啟你的 事業

完成課程獲得認證

開始
廣告