Skip to content

Matplotlib tick-label warning with pandas 3.1.0 #582

Open

Description

@kartiksubbarao

I got this warning after upgrading to pandas 3.1.0:

/path/to/python314/lib64/python3.14/site-packages/quantstats/_plotting/core.py:266: UserWarning: set_ticklabels() should only be used with a fixed number of ticks, i.e. after set_ticks() or using a FixedLocator. Otherwise, ticks may be mislabeled.
  ax.set_xticklabels(df.index.year)

Environment: Python 3.14, pandas 3.1.0, matplotlib 3.11.1, quantstats 0.0.86

Example

import pandas as pd
import quantstats as qs

r = pd.Series([0.1, -0.05], index=pd.date_range("2025", periods=2, freq="YE"))
qs.plots.yearly_returns(r, show=False)

I asked ChatGPT to suggest a fix and it said the following:


In quantstats/_plotting/core.py, replace:

Old:

df.plot(kind="bar", ax=ax, color=colors)

New:

df.set_axis(df.index.astype(str)).plot(kind="bar", ax=ax, color=colors)

No other changes required.

Pandas 3.1 introduced dynamic date ticks for datetime-indexed bar plots. Converting the plotting index to strings restores categorical tick positioning while preserving the original DataFrame and existing year-labeling logic.

This eliminates the warning without changing the plotted returns or chart appearance.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions