Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Quick handling of the ''fixedformatter should only be used together with fixedlocator'' error.
Next
Answered
Lee O'Connor
  • 31
Lee O'Connor
Asked: May 18, 20222022-05-18T19:16:09+00:00 2022-05-18T19:16:09+00:00In: python

Quick handling of the ”fixedformatter should only be used together with fixedlocator” error.

  • 31

. Advertisement .

..3..

. Advertisement .

..4..

I am new to python and searching the “fixedformatter should only be used together with fixedlocator” to understand it better. It seems it doesn’t work as expected when I used some suggestions before. Here is the command line I use:

def format_y_label_thousands(): # format y-axis tick labels formats
  ax = plt.gca()
  label_format = '{:,.0f}'
  ax.set_yticklabels([label_format.format(x) for x in ax.get_yticks().tolist()])
 
 def format_y_label_percent(): # format y-axis tick labels formats
  ax = plt.gca()
  label_format = '{:.1%}'
  ax.set_yticklabels([label_format.format(x) for x in ax.get_yticks().tolist()])

The error I’m getting is below:

UserWarning: FixedFormatter should only be used together with FixedLocator
  ax.set_yticklabels([label_format.format(x) for x in ax.get_yticks().tolist()])

Please give me the solution to this issue.

matplotlib
  • 2 2 Answers
  • 459 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-24T15:54:00+00:00Added an answer on June 24, 2022 at 3:54 pm

    The cause:

    After researching your problem for hours, I found that when you utilized an AutoLocator while changing the view or zooming, the labels frequently finished applying to the incorrect ticks. Therefore, this error happened.

    Solution:

    Let’s use FixedLocator to get rid of the warning (that is part of matplotlib.ticker). We have provided a code to plot three charts below. We use many formats for their axes. Although “set ticks” silences the warning, it alters the locations and labels of the real ticks (we spend a lot of time on finding that FixedLocator uses the same information but it still holds the original locations of ticks). To explore how each answer might impact the result, experiment with the x/ y’s.

    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}'
    
    # there is not anything done to ax1 because it is a "control chart."
    
    # fix 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])
    
    # fix 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])
    
    # fix xticks with FixedLocator but continues using MaxNLocator to prevent 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()

    THE FOLLOWING ARE THE OUTPUT CHARTS. Sample charts There is a few unused lines of code as the one above (we simply collect the xticks or yticks and set them again), only makes our application noisier. The warning should be removed, in our opinion. The contributors who administer matplotlib have their reasons for maintaining the warning, though, if you check into some of the “bug reports” (from links on the comments above/below; the problem is not truly an error: it is an update that is causing some issues). MATPLOTLIB ANCIENT VERSION: The warning messages could be troublesome if you utilize your console to manage negative code outputs, like we do. Therefore, downgrading matplotlib to version 3.2.2 is a way to put off having to deal with the problem. Here is the command used to downgrade matplotlib in Anaconda, which we use to manage our Python packages:

    conda install matplotlib=3.2.2

    Howerver, some of the listed versions might not be accessible. For example, in spite of appearance on matplotlib’s releases page, we were unable to install matplotlib 3.3.0: https://github.com/matplotlib/matplotlib/releases

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Emma Charles
    2022-05-25T20:52:42+00:00Added an answer on May 25, 2022 at 8:52 pm

    FixedLocator is not required if someone arrives here using axes.xaxis.set_ticklabels() or yaxis equivalent. You can also avoid this warning by using axes.xaxis.set_ticks(values_list) BEFORE axes.xaxis.set_ticklabels(labels_list).

    • 22
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.