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
  io.UnsupportedOperation: not readable RedSkeleton007 2 19,043 Nov-06-2023, 06:32 AM
Last Post: gpurdy
  matplotlib x-axis text move bottom upward jacklee26 3 1,118 May-31-2023, 04:28 AM
Last Post: jacklee26
  x-axis labels with Matplotlib Mark17 8 2,422 Mar-23-2022, 06:10 PM
Last Post: Mark17
  Function global not readable by 'main' fmr300 1 1,426 Jan-16-2022, 01:18 AM
Last Post: deanhystad
  matplotlib x axis range goes over the set range Pedroski55 5 3,373 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,569 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  Graphics Formatting - X-axis Notation and Annotations - Matplotlib silviover_junior 0 1,874 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,361 Mar-11-2021, 10:52 AM
Last Post: buran
  matplotlib x-axis wrong order SchroedingersLion 4 4,417 Feb-23-2021, 05:42 PM
Last Post: nilamo
  Help with Matplotlib and ordering the axis theliberalguy97 3 8,450 Dec-12-2020, 08:06 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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