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

วิธีตั้งค่า local rcParams หรือ rcParams สำหรับ figure เดียวใน matplotlib

ในการตั้งค่า local rcParams หรือ rcParams สำหรับ figure เดียวใน matplotlib สามารถทำตามขั้นตอนดังนี้:

ขั้นตอน

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots

  • เริ่มต้นตัวแปร N เก็บจำนวนข้อมูลตัวอย่าง

  • สร้างข้อมูล x และ y ด้วย numpy

  • คืนค่า context manager สำหรับเปลี่ยน rcParams ชั่วคราว

  • เพิ่ม subplot ใน figure ปัจจุบัน index 1

  • วาดข้อมูล x, y ด้วยเมธอด plot()

  • เพิ่ม subplot ใน figure ปัจจุบัน index 2

  • วาดข้อมูล x, y ด้วยเมธอด plot()

  • แสดงรูปด้วยเมธอด show()

ตัวอย่าง

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
N = 10
x = np.random.rand(N)
y = np.random.rand(N)
with plt.rc_context({"axes.grid": True, "grid.linewidth": 0.75, "lines.linestyle": 'dashed'}):
    plt.subplot(121)
    plt.plot(x, y)
plt.subplot(122)
plt.plot(x, y)
plt.show()

ผลลัพธ์

จะแสดงสอง subplots ซ้ายมี grid dashed ขวาไม่มี grid (default)

วิธีตั้งค่า local rcParams หรือ rcParams สำหรับ figure เดียวใน matplotlib