Issue
I have a pandas dataframe that uses strings as index. How can I set xlim for the x axis when my dataframe index is of type object? I tried adding two additional years one at the end and one at the beginning where all datasets are np.nan but that didn't work.
Here is the dataframe
The datatype of index is object
df.index
Out[52]: Index(['2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012'], dtype='object')
Here is the plot
So I would like to have some extra space on the x-axis in so the values for the fist and last year are better visible. What could I do?
EDIT:
Here is a minimal example using objects and not date objects as index
Solution
from __future__ import print_function
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
df = pd.DataFrame({'Foo': pd.Series([2,3,4], index=['2002', '2003', '2004'])})
fig, ax = plt.subplots()
df.plot(ax=ax)
which gets you the plot. To take a look at how the x-ticks are getting dealt with look at:
# note this is an AutoLocator
print(ax.xaxis.get_major_locator())
# note this is a FixedFormatter
print(ax.xaxis.get_major_formatter())
# these are the ticks that are used
ff = ax.xaxis.get_major_formatter()
print(ff.seq)
This means that if you pan around the tick labels will stay the same, but will be at random positions. This is the same problem as changing the xlim, the way pandas
sets up the plot initially the tick labels are completely decoupled from the data.
One (verbose) way to fix this is:
ax.xaxis.set_major_locator(mticker.FixedLocator(np.arange(len(df))))
ax.xaxis.set_major_formatter(mticker.FixedFormatter(df.index))
# note this is a FixedLocator
print(ax.xaxis.get_major_locator())
# note this is a FixedFormatter
print(ax.xaxis.get_major_formatter())
This will work no matter what you set your index to (strings vs dates)
I have created an issue with pandas https://github.com/pydata/pandas/issues/7612
Answered By - tacaswell
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.