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

วิธีพล็อต Arbitrary Markers บน Pandas Data Series ด้วย Matplotlib

การใช้ Marker กำหนดเองบน Pandas Series Plot

ใช้ pyplot.plot() โดยตรงกับ Series index และ values ระบุ marker

ขั้นตอนหลัก

  • ตั้งค่าขนาดรูปและ padding
  • สร้าง Pandas Series พร้อม datetime index
  • พล็อตด้วย plt.plot(ts.index, ts, '*', ls='dotted', color='red')
  • หมุน labels ด้วย tick_params(rotation=45)
  • แสดงผลด้วย show()

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

import pandas as pd
from matplotlib import pyplot as plt
import numpy as np

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

ts = pd.Series(np.random.randn(10), index=pd.date_range('2021-04-10', periods=10))
plt.plot(ts.index, ts, '*', ls='dotted', color='red')
plt.tick_params(rotation=45)
plt.show()

คำอธิบาย

  • '*': marker รูปดาว
  • ls='dotted': linestyle จุดไข่ปลา
  • ใช้ plt.plot() โดยตรง แทน ts.plot() เพื่อควบคุม marker ได้ดีกว่า