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
#2
One package. pyplot and dates are modules in matplotlib. DateFormatter is a class.

It is poor presentation to have 256 labels on a graph. Pyplot is giving bad results because your request is unreasonable. I don't know the frequency of your data, but you should consider only labelling the start of each year. Your plot should have 8 axis labels, not 256.

It is common for plots to not label the start of rend of the plot. Without seeing the data I cannot tell you exactly why the last point is not labeled.
Reply
#3
Life is a journey, you don’t care about the destination, you care about the scenery along the way and the mood of seeing the scenery.
Others laughed at me as crazy, I laughed at others and couldn't see through.
on the thought of his buddy zhuge qing, zhang chulan and feng baobao came to resolve the case with the help of "anywhere" business enterprise. how does zhang chulan display his competencies to assist the king trap the black hand backstage? what wonderful overall performance will zhuge qing, wang ye and others have? who're the people who covet the "8 wonders"? "under one guy-becoming a member of the world" may be introducedsoon!

what is a sponsored ad
Reply
#4
I played around with the "too many labels" problem,
causing them to be untidy, or unreadable.
The solution is of course, less labels. The rotation "vertical" option helps. Smaller font also.
I ended up with a python/matplotlib solution that allows
to print only "every x-th" label from a vector of labels.
For evry 12-th observation the format looks like this:
lbls[::12]
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#5
I should have clarified... I don't need 256 dates on the x-axis. This normally happens when I have a dataframe with all cells filled in. One column is date and one column is some dependent variable. I would like a way to tell Python to only print the dates every x rows or beginning of every month, or maybe Y dates for the whole graph (divided accordingly), etc. Any of these could be fine.

My frustration is in not knowing a single approach to plt that will accommodate most of the frequent things I will need to do. This is how my data usually is: a frequency of dates and dependent variable(s) for each date. The data can go on for months to years and I may want to plot it all--but I certainly don't need all the dates printed.

One thing I do need, though, is for a date label to line up vertically with the y-value being plotted. My ideal solution here will not use an additional Class or anything not in the stock package because I can't believe this is anything too complicated that the stock package shouldn't be able to handle. I suspect that's what causing this problem.
Reply
#6
(Mar-01-2022, 07:54 AM)DPaul Wrote: I played around with the "too many labels" problem,
causing them to be untidy, or unreadable.
The solution is of course, less labels. The rotation "vertical" option helps. Smaller font also.
I ended up with a python/matplotlib solution that allows
to print only "every x-th" label from a vector of labels.
For evry 12-th observation the format looks like this:
lbls[::12]
Paul

I tried this:

ax.plot(btstats['Date'::7], btstats['Cum.PnL'])
The result is: TypeError: cannot do slice indexing on Int64Index with these indexers [Cum.PnL] of type str
Reply
#7
This is much better:

datetimes = pd.to_datetime(btstats['Date'])
btstats['Date'] = datetimes

fig, ax = plt.subplots()
ax.plot(btstats['Date'], btstats['Cum.PnL'])
ax.set_xlabel('Date')
ax.set_ylabel('Cumulative PnL')
The 'Date' column was type object. Converting to datetime lets matplotlib automatically scale it and it now prints just every two months, which is fine. I could probably customize that somehow... I'll try and figure that out but if not then this is readable and clean.

The dates also line up with the data, now, and it plots through the last date in the dataframe.
Reply
#8
Quote:I tried this:

ax.plot(btstats['Date'::7], btstats['Cum.PnL'])
The result is: TypeError: cannot do slice indexing on Int64Index with these indexers [Cum.PnL] of type str

I said it works on a vector of labels, not a single string. Cool
Paul
Mark17 likes this post
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  io.UnsupportedOperation: not readable RedSkeleton007 2 18,571 Nov-06-2023, 06:32 AM
Last Post: gpurdy
  matplotlib x-axis text move bottom upward jacklee26 3 928 May-31-2023, 04:28 AM
Last Post: jacklee26
  x-axis labels with Matplotlib Mark17 8 2,124 Mar-23-2022, 06:10 PM
Last Post: Mark17
  Function global not readable by 'main' fmr300 1 1,295 Jan-16-2022, 01:18 AM
Last Post: deanhystad
  matplotlib x axis range goes over the set range Pedroski55 5 3,103 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,232 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  Graphics Formatting - X-axis Notation and Annotations - Matplotlib silviover_junior 0 1,758 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,161 Mar-11-2021, 10:52 AM
Last Post: buran
  matplotlib x-axis wrong order SchroedingersLion 4 4,177 Feb-23-2021, 05:42 PM
Last Post: nilamo
  Help with Matplotlib and ordering the axis theliberalguy97 3 7,741 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