1) Я предполагаю, что ваша ось x
содержит string
, а не datetime
. Затем, прежде чем строить график, я конвертирую его, как показано ниже.
x=[datetime.strptime(xi, "%d.%m.%y") for xi in x]
2) Если вы выберете MonthLocator
, вы не сможете получить его как 01 марта ... Таким образом, переключите его с помощью DayLocator
.
locator = mdates.DayLocator()
3) Этот код необязателен для использования кода очистки. Вам не нужно X
.
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(fmt)
ax.xaxis.set_tick_params(rotation=30)
Пример кода здесь.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime
x=["01.03.20", "02.03.20", "04.03.20", "05.03.20"]
x=[datetime.strptime(xi, "%d.%m.%y") for xi in x]
y=[10, 10, 15,16]
fig, ax = plt.subplots()
ax.scatter(x, y, s=100, c='C0')
ax.plot(x, y, ls='-', c='C0')
locator = mdates.DayLocator()
fmt = mdates.DateFormatter('%b-%d')
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(fmt)
ax.xaxis.set_tick_params(rotation=30)
ax.set_xlim(x[0],x[3])
plt.show()
Пример результата здесь.
