Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FixedFormatter_shoul_only_be_used_together_with_FixedLocator: How to deal with this warning in matplotlib?

I have a simple bar chart with a line chart over it.

import numpy as np
import matplotlib.pyplot as plt

x = np.array(["one", "two", "three", "four"])
a = np.array([1, 2, 3, 4])
b = np.array([2, 4, 3, 1])

fig, ax1 = plt.subplots()

ax2 = ax1.twinx()
ax1.bar(x, a, color="g")
ax2.plot(x, b, color="r")

# Problem is here.
ax1.set_xticklabels(x, rotation="vertical", size=12)

plt.show()

When I run it, it works fine. But I get this warning:

"""
<ipython-input-65-9b40369b760b>:15: UserWarning: FixedFormatter should only be used together with 
FixedLocator
  ax1.set_xticklabels(x, rotation="vertical", size=12)
"""

All I need to know is how to avoid this warning.

like image 423
Three Point 14 Avatar asked Oct 30 '25 15:10

Three Point 14


1 Answers

It seems to be a bug with the latest version. You can read more about it here.

Their solution seems to be to set xticks before you set the lables, so for you example, we just add this before your labels:

ax1.set_xticks(x)
ax1.set_xticklabels(x, rotation="vertical", size=12)

The warning is removed on my side then at least.

like image 98
Hampus Larsson Avatar answered Nov 01 '25 04:11

Hampus Larsson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!