Python Forum
x-axis labels with Matplotlib
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
x-axis labels with Matplotlib
#2
You should post your code so others can run it. I tried to duplicate from the description.
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
Mark17 likes this post
Reply


Messages In This Thread
x-axis labels with Matplotlib - by Mark17 - Mar-22-2022, 06:26 PM
RE: x-axis labels with Matplotlib - by deanhystad - Mar-22-2022, 09:06 PM
RE: x-axis labels with Matplotlib - by Mark17 - Mar-23-2022, 01:26 PM
RE: x-axis labels with Matplotlib - by deanhystad - Mar-23-2022, 02:56 PM
RE: x-axis labels with Matplotlib - by Mark17 - Mar-23-2022, 03:58 PM
RE: x-axis labels with Matplotlib - by deanhystad - Mar-23-2022, 05:06 PM
RE: x-axis labels with Matplotlib - by Mark17 - Mar-23-2022, 05:45 PM
RE: x-axis labels with Matplotlib - by deanhystad - Mar-23-2022, 05:58 PM
RE: x-axis labels with Matplotlib - by Mark17 - Mar-23-2022, 06:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  matplotlib x-axis text move bottom upward jacklee26 3 1,161 May-31-2023, 04:28 AM
Last Post: jacklee26
  How can histogram bins be separated and reduce number of labels printed on x-axis? cadena 1 998 Sep-07-2022, 09:47 AM
Last Post: Larz60+
  How to avoid the extra set of y-axis labels? Mark17 9 2,692 May-17-2022, 06:26 PM
Last Post: Mark17
  Floor division problem with plotting x-axis tick labels Mark17 5 2,287 Apr-03-2022, 01:48 PM
Last Post: Mark17
  How to make x-axis readable with matplotlib Mark17 7 4,183 Mar-01-2022, 04:30 PM
Last Post: DPaul
  matplotlib x axis range goes over the set range Pedroski55 5 3,431 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 4,667 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  Graphics Formatting - X-axis Notation and Annotations - Matplotlib silviover_junior 0 1,913 Mar-17-2021, 01:19 PM
Last Post: silviover_junior
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,396 Mar-11-2021, 10:52 AM
Last Post: buran
  matplotlib x-axis wrong order SchroedingersLion 4 4,449 Feb-23-2021, 05:42 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020