0

This is my code for plotting

import matplotlib.pyplot as plt
nb = [days_data[d]["Netzeinspeisung"] for d in days_data]
days = [d for d in days_data]
plt.plot(days, nb, marker="+")
plt.grid()
days_label = []
for d in days:
    if int(d.split(".")[0])%15 == 0 or d.split(".")[0] == "01":
        days_label.append(d)
    else:
        days_label.append(" ")
plt.xticks(ticks=range(len(days_label)), labels=days_label, rotation=30)
plt.show()

How it looks

What I want to do is making the ticks with a date longer

I tried to somehow do it with Minor and Major ticks but I don't know how to do it

mozway
  • 194,879
  • 13
  • 39
  • 75
Pythonwolf
  • 109
  • 1
  • 9

1 Answers1

0

I suggest to use matplotlib.dates and its special locators and formatters, and then customize x axis:

from datetime import date, timedelta
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import random

# fake datas
day0 = date(2022, 1, 30)
days = [day0 + timedelta(days=i) for i in range(53)]
N = len(days)
nrd = [random.randint(0, 30) for _ in range(N)]

# datas line
fig, ax = plt.subplots()
ax.plot(days, nrd, marker="+")
ax.grid()

# ticks
ax.xaxis.set_major_locator(mdates.DayLocator([1, 15]))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y'))
ax.xaxis.set_minor_locator(mdates.DayLocator(interval=1))
ax.tick_params(axis='x', which='major', width=2, length=5, labelrotation=30)

#
plt.show()

enter image description here

david
  • 1,302
  • 1
  • 10
  • 21