Mar-22-2022, 09:06 PM
You should post your code so others can run it. I tried to duplicate from the description.
Code for getting fridays in "%m-%d" format.
import random import matplotlib.pyplot as plt import numpy as np from datetime import date, timedelta def friday(year): day = date(year, 1, 1) day += timedelta(days=(4 - day.weekday())%7) week = timedelta(weeks=1) while day.year == year: yield day day += week random_pnl_list = [] for i in range(20): random_pnl_list.append(random.randint(-1000,1000)) cumul_pnl_from_random = list(np.cumsum(random_pnl_list)) fridays = list(friday(2017))[:len(cumul_pnl_from_random)] plt.plot(fridays, cumul_pnl_from_random) plt.show()My guess is matplotlib is doing the same kind of thing it does with numbers; picking nice start and stop values that it can divide into evenly sized intervals appropriate for the plot. When you convert the dates to strings they become just that, strings. Matplotlib does not know what they mean, does not know how they should be spaced, and just spaces them evenly without worrying about the "value" of the first or last x value.
Code for getting fridays in "%m-%d" format.
def friday(year): day = date(year, 1, 1) day += timedelta(days=(4 - day.weekday())%7) week = timedelta(weeks=1) while day.year == year: yield day.strftime('%m-%d') # yield day day += week