Python Forum
How to make x-axis readable with matplotlib
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make x-axis readable with matplotlib
#1
I've had this problem before. I'll have dates in a df column and plotting one year with 256 dates results in a black blur on the bottom.

Now... I wound up with this after searching the Net, finding a number of different pages, and incorporating some different ones:

fig, ax = plt.subplots()
ax.plot(btstats['Date'], btstats['Cum.PnL'])
ax.set_xlabel('Date')
ax.set_ylabel('Cumulative PnL')
date_form = DateFormatter("%b")
ax.xaxis.set_major_formatter(date_form)
#months = mdates.MonthLocator()
ax.xaxis.set_major_locator(mdates.MonthLocator(interval = 1))
#ax.set_xticklabels(ax.get_xticks(), rotation = 90)
plt.tight_layout()
plt.show()
It now plots just 3-letter abbreviations for each month but the labels don't line up right with the data. The last label shown is Sep whereas the data go through the end of Dec. Why might that be?

And in general, I don't like this solution because at the top, I ended up with:

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.dates import DateFormatter
I had to import three different packages for this? Seems like often when I find a plt solution that solves one problem, I have to import something different. That's frustrating because when I learned plt, it was said to be a highly effective, efficient library, etc.

Any input is appreciated!
Reply


Messages In This Thread
How to make x-axis readable with matplotlib - by Mark17 - Feb-28-2022, 10:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Storing data in readable place user_404_lost_and_found 4 1,478 Jul-22-2024, 06:14 AM
Last Post: Pedroski55
  io.UnsupportedOperation: not readable RedSkeleton007 2 21,582 Nov-06-2023, 06:32 AM
Last Post: gpurdy
  matplotlib x-axis text move bottom upward jacklee26 3 2,184 May-31-2023, 04:28 AM
Last Post: jacklee26
  x-axis labels with Matplotlib Mark17 8 4,534 Mar-23-2022, 06:10 PM
Last Post: Mark17
  Function global not readable by 'main' fmr300 1 2,141 Jan-16-2022, 01:18 AM
Last Post: deanhystad
  matplotlib x axis range goes over the set range Pedroski55 5 6,636 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 6,913 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  Graphics Formatting - X-axis Notation and Annotations - Matplotlib silviover_junior 0 2,557 Mar-17-2021, 01:19 PM
Last Post: silviover_junior
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 4,314 Mar-11-2021, 10:52 AM
Last Post: buran
  matplotlib x-axis wrong order SchroedingersLion 4 5,864 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