Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีปรับคุณภาพภาพ Matplotlib ให้ดียิ่งขึ้น


ใช้ DPI (dots per inch) สูงกว่า 600 และแนะนำให้บันทึกเป็น .pdf หรือ .eps

ขั้นตอนการทำงาน

  • กำหนดขนาดรูปภาพและปรับระยะห่างระหว่างและรอบๆ subplots
  • สร้างข้อมูล 2D raster ด้วย np.array
  • แสดงข้อมูลเป็นภาพ
  • บันทึกรูปภาพด้วย savefig() พร้อม dpi=1200 และ format .eps
  • ใช้เมธอด show() เพื่อแสดงรูปภาพ

ตัวอย่างโค้ด

import numpy as np
from matplotlib import pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

data = np.array([[0.1, 0.7, 0.6, 0.3], [0.2, 0.6, 0.5, 0.2], [0.8, 0.3, 0.80, 0.01], [0.3, 0.4, 0.2, 0.1]])
plt.imshow(data, interpolation="nearest", cmap="RdYlGn_r")
plt.savefig("myimage.eps", dpi=1200)
plt.show()

ผลลัพธ์

วิธีปรับคุณภาพภาพ Matplotlib ให้ดียิ่งขึ้น