Skip to content

plotting with index of objects should use FixedLocator #7612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tacaswell opened this issue Jun 29, 2014 · 7 comments · Fixed by #26185
Closed

plotting with index of objects should use FixedLocator #7612

tacaswell opened this issue Jun 29, 2014 · 7 comments · Fixed by #26185

Comments

@tacaswell
Copy link
Contributor

As pointed out here (https://stackoverflow.com/questions/24468333/set-xlim-for-pandas-matplotlib-where-index-is-string/24477138#24477138) if the index of a data frame is objects (that is not, the index obviously convertible to scalars and the data is being plotted against range(len(df))), the ticklabels are set to be string representations of the objects (I presume via set_xticklabels) but the locator is still a AutoLocator which means if you change the xlimits or pan/zoom the axes, the ticklabels become de-coupled from the data.

A solution is to also use a fixed locator which will pin the ticks to only be on the index locations.

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)

ax.xaxis.set_major_locator(mticker.FixedLocator(np.arange(len(df))))
ax.xaxis.set_major_formatter(mticker.FixedFormatter(df.index))

I can make changes to the code if someone points me to where the relevant section is.

@danielballan
Copy link
Contributor

I believe the change should be made in this method of the MPLPlot class, from which all plots (Line, Bar, Scatter, etc.) inherit.

https://github.com/pydata/pandas/blob/master/pandas/tools/plotting.py#L1045

@jreback
Copy link
Contributor

jreback commented Oct 20, 2015

closing as stale. pls reopen if still an issue.

@jreback jreback closed this as completed Oct 20, 2015
@tacaswell
Copy link
Contributor Author

This is still a bug and should be re-opened.

@jorisvandenbossche
Copy link
Member

When someone tries to fix this, make sure to test whether #2980, #11465 and #15912 are then solved.

@TomAugspurger @sinhrks do you have an idea where this should best be handled? Somewhere in _make_plot?

@TomAugspurger
Copy link
Contributor

Yeah, _make_plot is where the change is needed, but if we're fixing it in MPLPlot, then we'll need to do some refactoring.

@firasm
Copy link

firasm commented May 15, 2018

FYI: This still appears to be an issue and drove me crazy till I found this!

@nrebena
Copy link
Contributor

nrebena commented Apr 22, 2019

So I think i found the place where to put the change suggested by @tacaswell, and it fix #7612, #15912, #22334.
Regarding issues #2980 and #11465 cited by @jorisvandenbossche , I do not think they are related to this issue.

nrebena pushed a commit to nrebena/pandas that referenced this issue Apr 23, 2019
nrebena pushed a commit to nrebena/pandas that referenced this issue Apr 24, 2019
Separate the test for pandas-dev#7612 and pandas-dev#22334, that are about string index,
and test for pandas-dev#15912, about multiindex.
@jreback jreback modified the milestones: Contributions Welcome, 0.25.0 Apr 28, 2019
jreback pushed a commit that referenced this issue Apr 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants