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 a question.

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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8968807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:30:19+00:00 2026-06-15T17:30:19+00:00

I am trying to have two inter-depedent x-axis in a matplotlib imshow() plot. I

  • 0

I am trying to have two inter-depedent x-axis in a matplotlib imshow() plot. I have bottom x-axis as the radius squared and I want the top as just the radius. I have tried so far:

ax8 = ax7.twiny()
ax8._sharex = ax7
fmtr = FuncFormatter(lambda x,pos: np.sqrt(x) )
ax8.xaxis.set_major_formatter(fmtr)
ax8.set_xlabel("Radius [m]")

where ax7 is the y-axis and the bottom x-axis (or radius squared). Instead of getting the sqrt (x_bottom) as the ticks at the top I just get a range from 0 to 1. How can I fix this?

Thanks a lot in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:30:20+00:00Added an answer on June 15, 2026 at 5:30 pm

    You’re misunderstanding what twiny does. It makes a completely independent x-axis with a shared y-axis.

    What you want to do is have a different formatter with a linked axis (i.e. sharing the axis limits but nothing else).

    The simple way to do this is to manually set the axis limits for the twinned axis:

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.ticker import FuncFormatter
    
    fig, ax1 = plt.subplots()
    ax1.plot(range(10))
    
    ax2 = ax1.twiny()
    formatter = FuncFormatter(lambda x, pos: '{:0.2f}'.format(np.sqrt(x)))
    ax2.xaxis.set_major_formatter(formatter)
    
    ax2.set_xlim(ax1.get_xlim())
    
    plt.show()
    

    enter image description here

    However, as soon as you zoom or interact with the plot, you’ll notice that the axes are unlinked.

    You could add an axes in the same position with both shared x and y axes, but then the tick formatters are shared, as well.

    Therefore, the easiest way to do this is using a parasite axes.

    As a quick example:

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.ticker import FuncFormatter
    from mpl_toolkits.axes_grid1.parasite_axes import SubplotHost
    
    fig = plt.figure()
    ax1 = SubplotHost(fig, 1,1,1)
    fig.add_subplot(ax1)
    
    ax2 = ax1.twin()
    
    ax1.plot(range(10))
    
    formatter = FuncFormatter(lambda x, pos: '{:0.2f}'.format(np.sqrt(x)))
    ax2.xaxis.set_major_formatter(formatter)
    
    plt.show()
    

    Both this and the previous plot will look identical at first. The difference will become apparent when you interact (e.g. zoom/pan) with the plot.

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

Sidebar

Related Questions

I have two divs named arrow and inner. I am trying to control the
I am trying to have two submit buttons in my form - one accepts
I am trying to have two data series plotted in one graph as boxes
i have two tables i am trying to get information from. login table -
I have two PNGs that I am trying to combine into a PDF using
I have two very similar programs each trying to run two threads OddThread and
I have two arrayLists and I am trying to subtract one arrayList from another.
I have two arrays of data that I'm trying to amalgamate. One contains actual
I have two tables: hotels and hotel_rooms I'm trying to get a table with
I have two relatively complex queries that I am trying to join together into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.