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

พลอตกราฟด้วย Style Sheet fivethirtyeight ใน Matplotlib


เพื่อใช้ fivethirtyeight stylesheet

  • ตั้งค่าขนาดรูปและ padding ระหว่าง subplots
  • ใช้ plt.style.use() เรียก 'fivethirtyeight'
  • สร้างข้อมูล x ด้วย numpy
  • สร้าง figure และ subplots ด้วย subplots()
  • พลอตสามเส้นโค้งด้วย plot()
  • ตั้งชื่อเรื่อง
  • แสดงรูปด้วย show()

ตัวอย่าง

import matplotlib.pyplot as plt
import numpy as np

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

plt.style.use('fivethirtyeight')
x = np.linspace(0, 10)
fig, ax = plt.subplots()
ax.plot(x, np.sin(x) + x + np.random.randn(50))
ax.plot(x, np.sin(x) + 0.5 * x + np.random.randn(50))
ax.plot(x, np.sin(x) + 2 * x + np.random.randn(50))
ax.set_title("'fivethirtyeight' style sheet")
plt.show()

ผลลัพธ์

พลอตกราฟด้วย Style Sheet fivethirtyeight ใน Matplotlib