Skip to content

VIS/BUG DatetimeIndex with freq=None plot return rotated xtickslabels #15474

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
weber-s opened this issue Feb 22, 2017 · 3 comments
Closed

VIS/BUG DatetimeIndex with freq=None plot return rotated xtickslabels #15474

weber-s opened this issue Feb 22, 2017 · 3 comments

Comments

@weber-s
Copy link

weber-s commented Feb 22, 2017

Code Sample

file = 'test.csv' #with a "date" column
df1  = pd.read_csv(file, index_col="date", parse_dates=["date"])
df1.plot()

idx =pd.date_range('2000-01-01','2003-01-01', freq="6d")
df2 = pd.DataFrame(index=idx, data=np.random.randn(len(idx),1))
df2.plot()

Problem description

When plotting a time indexed series, we expect no rotation in x labels. It does not work when importing data with

df  = pd.read_csv(file, index_col="date", parse_dates=["date"])

where the rotation occurs. Moreover, set rot=0 gives a strange behavior with label not centered on ticks.

The only difference is that freq=None in the first case.

Expected Output

No rotation by default.

Here is what we have for df1.plot() (Problem) :
df1

Here is what we have for df1.plot(rot=0) (Problem) :
df1rot

Here is what we have for df2.plot() (Ok) :
df2

Details

May be related to :

INSTALLED VERSIONS ------------------ commit: None python: 3.6.0.final.0 python-bits: 64 OS: Linux OS-release: 4.9.9-1-ARCH machine: x86_64 processor: byteorder: little LC_ALL: None LANG: fr_FR.UTF-8 LOCALE: fr_FR.UTF-8

pandas: 0.19.2
nose: None
pip: None
setuptools: 34.2.0
Cython: None
numpy: 1.12.0
scipy: 0.18.1
statsmodels: None
xarray: None
IPython: 5.1.0
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 2.0.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: 0.10.3
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.5
boto: None
pandas_datareader: None

@jorisvandenbossche
Copy link
Member

The rotation is done on purpose, as you would otherwise get overlapping labels (like in your second example plot, only without the non-centered labels).

So there are two ways pandas plots datetimes under the hood: ones with a regular frequence, and the ones without regular frequency (that is the reason you see a difference for frames with a different freq). The problem is that only for the regular time series, we have custom label formatting (your third plot). For the non-regular, we just use matplotlib default formatting, with this difference that we apply a rotation by default to have non-overlapping labels.
See #15071 for a longer explanation.

The shift in the labels (non-centered) when using rot=0 is a bit annoying.

@weber-s
Copy link
Author

weber-s commented Feb 23, 2017

Thanks for the explanation (personally, I am in favour of passing all plots in period base plotting, it looks nicer).

The shift in the labels (non-centered) when using rot=0 is a bit annoying.

I spoted the problem in format_date_labels:

def format_date_labels(ax, rot):
    # mini version of autofmt_xdate
    try:
        for label in ax.get_xticklabels():
            label.set_ha('right') # here is the problem
            label.set_rotation(rot)
        fig = ax.get_figure()
        fig.subplots_adjust(bottom=0.2)
    except Exception:  # pragma: no cover
        pass

with rot=0 the alignement should be center. However I have no idea how to solve it.

I think it's a feature (having the end of the labels on the ticks), however it is a bit confusing without rotation.

@mroeschke
Copy link
Member

Seems like this behavior is intended. Going to close for now, but happy to reopen if there is future interest on an enhancement on this behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants