close

[Solved] UserWarning: FixedFormatter should only be used together with FixedLocator

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error UserWarning: FixedFormatter should only be used together with FixedLocator in Python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How UserWarning: FixedFormatter should only be used together with FixedLocator Error Occurs?

Today I get the following error UserWarning: FixedFormatter should only be used together with FixedLocator in Python.

How To Solve UserWarning: FixedFormatter should only be used together with FixedLocator Error ?

  1. How To Solve UserWarning: FixedFormatter should only be used together with FixedLocator Error ?

    To Solve UserWarning: FixedFormatter should only be used together with FixedLocator Error just use axis.set_xticks([labels])

Solution 1

WORKAROUND:

The way to avoid the warning is to use FixedLocator (that is part of matplotlib.ticker). Below I show a code to plot three charts. I format their axes in different ways. Note that the “set_ticks” silence the warning, but it changes the actual ticks locations/labels (it took me some time to figure out that FixedLocator uses the same info but keeps the ticks locations intact). You can play with the x/y’s to see how each solution might affect the output.

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as mticker

mpl.rcParams['font.size'] = 6.5

x = np.array(range(1000, 5000, 500))
y = 37*x

fig, [ax1, ax2, ax3] = plt.subplots(1,3)

ax1.plot(x,y, linewidth=5, color='green')
ax2.plot(x,y, linewidth=5, color='red')
ax3.plot(x,y, linewidth=5, color='blue')

label_format = '{:,.0f}'

# nothing done to ax1 as it is a "control chart."

# fixing yticks with "set_yticks"
ticks_loc = ax2.get_yticks().tolist()
ax2.set_yticks(ax1.get_yticks().tolist())
ax2.set_yticklabels([label_format.format(x) for x in ticks_loc])

# fixing yticks with matplotlib.ticker "FixedLocator"
ticks_loc = ax3.get_yticks().tolist()
ax3.yaxis.set_major_locator(mticker.FixedLocator(ticks_loc))
ax3.set_yticklabels([label_format.format(x) for x in ticks_loc])

# fixing xticks with FixedLocator but also using MaxNLocator to avoid cramped x-labels
ax3.xaxis.set_major_locator(mticker.MaxNLocator(3))
ticks_loc = ax3.get_xticks().tolist()
ax3.xaxis.set_major_locator(mticker.FixedLocator(ticks_loc))
ax3.set_xticklabels([label_format.format(x) for x in ticks_loc])

fig.tight_layout()
plt.show()

OUTPUT CHARTS:

Sample charts

Obviously, having a couple of idle lines of code like the one above (I’m basically getting the yticks or xticks and setting them again) only adds noise to my program. I would prefer that the warning was removed. However, look into some of the “bug reports” (from links on the comments above/below; the issue is not actually a bug: it is an update that is generating some issues), and the contributors that manage matplotlib have their reasons to keep the warning.

OLDER VERSION OF MATPLOTLIB: If you use your Console to control critical outputs of your code (as I do), the warning messages might be problematic. Therefore, a way to delay having to deal with the issue is to downgrade matplotlib to version 3.2.2. I use Anaconda to manage my Python packages, and here is the command used to downgrade matplotlib:

conda install matplotlib=3.2.2

Not all listed versions might be available. For instance, couldn’t install matplotlib 3.3.0 although it is listed on matplotlib’s releases page: https://github.com/matplotlib/matplotlib/releases

Solution 2

just use axis.set_xticks([labels])

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment