ในการพล็อตกรอบข้อมูลหลายดัชนีของ Pandas ด้วย xticks ทั้งหมด เราสามารถทำตามขั้นตอนต่อไปนี้ -
- กำหนดขนาดรูปและปรับช่องว่างภายในระหว่างและรอบๆ แผนผังย่อย
- สร้างค่าดัชนีด้วยข้อมูลตัวอย่าง 1,000 รายการ
- สร้าง ndarray หนึ่งมิติ พร้อมป้ายแกน
- หาค่าเฉลี่ยของชุดข้อมูล
- พล็อต ก ดาต้าเฟรม
- กำหนดขีดและขีดบนแกนปัจจุบัน
- หากต้องการแสดงรูป ให้ใช้ show() วิธีการ
ตัวอย่าง
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
idx = pd.date_range("2020-01-01", periods=1000)
val = np.random.rand(1000)
s = pd.Series(val, idx)
g = s.groupby([s.index.year, s.index.month]).mean()
ax = g.plot()
ax.set_xticks(range(len(g)))
ax.set_xticklabels(["%s-%02d" % item for item in g.index.tolist()],
rotation=45, ha='center')
plt.show() ผลลัพธ์
