在 Matplotlib 中繪製日期

Salman Mehmood 2023年1月30日

在 Matplotlib 中使用 `matplotlib.dates` 繪製日期

``````from matplotlib import pyplot as plot

# A matplotlib date tool
import matplotlib.dates as mpl_dates

from datetime import datetime, timedelta
``````

``````# A list of dates for seven dates
dates = [
datetime(2021, 10, 10),
datetime(2021, 10, 11),
datetime(2021, 10, 12),
datetime(2021, 10, 13),
datetime(2021, 10, 14),
datetime(2021, 10, 15),
datetime(2021, 10, 16),
]

# A data list
y1 = [1, 10, 3, 2, 5, 16, 7]
``````

`plot_date()` 方法有助於繪製日期，因此我們呼叫此方法並傳遞一個 `dates` 引數作為 x 軸，將 `y1` 引數作為 y 軸。

``````plot.plot_date(dates, y1, linestyle="solid")
``````

``````plot.gcf().autofmt_xdate()
``````

``````date_format = mpl_dates.DateFormatter("%b, %d %Y")
# Get the access of current figure
plot.gca().xaxis.set_major_formatter(date_format)
``````

`tight_layout()` 方法為我們的繪圖新增了填充。

``````plot.tight_layout()
``````

``````from matplotlib import pyplot as plot

# A matplotlib date tool
import matplotlib.dates as mpl_dates

from datetime import datetime, timedelta

# A list of dates for seven dates
dates = [
datetime(2021, 10, 10),
datetime(2021, 10, 11),
datetime(2021, 10, 12),
datetime(2021, 10, 13),
datetime(2021, 10, 14),
datetime(2021, 10, 15),
datetime(2021, 10, 16),
]

# A data list
y1 = [1, 10, 3, 2, 5, 16, 7]

plot.plot_date(dates, y1, linestyle="solid")

date_format = mpl_dates.DateFormatter("%b, %d %Y")
# Get the access of current figure
plot.gcf().autofmt_xdate()
# Get the access of current figure
plot.gca().xaxis.set_major_formatter(date_format)
# Make responsive
plot.tight_layout()
plot.show()
``````

在 Matplotlib 中使用 CSV 檔案中的資料繪製日期

``````import pandas as pd
``````

``````data = pd.read_csv("data.csv")
``````

``````data["Date"] = pd.to_datetime(data["Date"])
data.sort_values("Date", inplace=True)

price_date = data["Date"]
price_close = data["Close"]
``````

``````plot.plot_date(price_date, price_close, linestyle="solid")
``````

``````from matplotlib import pyplot as plot

# A matplotlib date tool
import matplotlib.dates as mpl_dates

from datetime import datetime, timedelta
import pandas as pd

data["Date"] = pd.to_datetime(data["Date"])
data.sort_values("Date", inplace=True)

price_date = data["Date"]
price_close = data["Close"]

plot.plot_date(price_date, price_close, linestyle="solid")

date_format = mpl_dates.DateFormatter("%b, %d %Y")
# Get the access of current figure
plot.gcf().autofmt_xdate()
# Get the access of current figure
plot.gca().xaxis.set_major_formatter(date_format)

plot.title("Bitcoin Prices")
plot.xlabel("Date")
plot.ylabel("Closing Price")

# Make responsive
plot.tight_layout()
plot.show()
``````

Hello! I am Salman Bin Mehmood(Baum), a software developer and I help organizations, address complex problems. My expertise lies within back-end, data science and machine learning. I am a lifelong learner, currently working on metaverse, and enrolled in a course building an AI application with python. I love solving problems and developing bug-free software for people. I write content related to python and hot Technologies.