Python Forum
x-axis labels with Matplotlib
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
x-axis labels with Matplotlib
#3
Done with the Fri_2017 list, x-axis labels are periodic but asymmetric as the 22nd and 1st are close together followed by a longer space and then another 22nd and 1st, etc. Is this "evenly sized intervals appropriate for the plot?" I would have thought if left to auto scale, matplotlib would have selected intervals that were really evenly spaced out (e.g. every Z x-value increments and since an x-value here is one week, that translates to every Z weeks).

Incidentally, last time I posted a question about matplotlib I didn't get a response and I figured that might have been because the code was too long and messy for people to bother wading through.

Sorry you had to take the time to reconstruct everything, Dean. My code here went something like this:

from datetime import datetime
from datetime import timedelta
import random
import numpy as np
import matplotlib.pyplot as plt

Fri_2017 = [] #2017_Fri gives "invalid decimal literal" at the underscore
random_pnl_list = []
cumul_pnl_from_random = []

start_date = datetime(2017,1,1) #not datetime.datetime()
while True: #True must be capitalized
    if start_date.weekday() == 4:
        Fri_2017.append(start_date)
        break
    else:
        start_date += timedelta(days=1)
for i in range(19):
    Fri_2017.append(Fri_2017[-1] + timedelta(days=7))

converted_Fri_2017 = [d.strftime('%Y-%m-%d') for d in Fri_2017] #list comprehension
print(converted_Fri_2017)

for i in range(20):
    random_pnl_list.append(random.randint(-1000,1000))

cumul_pnl_from_random = list(np.cumsum(random_pnl_list)) #np returns array but this MUCH easier than doing loop to compute
print(random_pnl_list)
print(cumul_pnl_from_random)

fig, [ax1, ax2] = plt.subplots(2)

plt.sca(ax1)
plt.plot(Fri_2017,cumul_pnl_from_random)
plt.xticks(rotation=90)
plt.sca(ax2)
plt.plot(converted_Fri_2017,cumul_pnl_from_random)
plt.xticks(rotation=90)

plt.tight_layout()
plt.show()
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