Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách vẽ marker tùy ý trên Pandas Series bằng Matplotlib?

Để vẽ marker tùy ý trên Pandas Series, ta dùng pyplot.plot() với markers.

Các bước

  • Thiết lập kích thước hình và điều chỉnh khoảng đệm subplot.
  • Tạo Pandas Series với nhãn trục (bao gồm timeseries).
  • Vẽ index series bằng plot() với linestyle="dotted".
  • Dùng tick_params() xoay nhãn chồng chéo.
  • Hiển thị hình bằng show().

Ví dụ

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()

Kết quả