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 6684347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:55:47+00:00 2026-05-26T04:55:47+00:00

Define data x = np.linspace(0,2*np.pi,100) y = 2*np.sin(x) Plot fig = plt.figure() ax =

  • 0

Define data

x = np.linspace(0,2*np.pi,100)
y = 2*np.sin(x)

Plot

fig = plt.figure()
ax = plt.axes()
fig.add_subplot(ax)
ax.plot(x,y)

Add second axis

newax = plt.axes(axisbg='none')

Gives me ValueError: Unknown element o, even though it does the same thing as what I am about to describe. I can also see that this works (no error) to do the same thing:

newax = plt.axes()
fig.add_subplot(newax)
newax.set_axis_bgcolor('none')

However, it turns the background color of the original figure “gray” (or whatever the figure background is)? I don’t understand, as I thought this would make newax transparent except for the axes and box around the figure. Even if I switch the order, same thing:

plt.close('all')
fig = plt.figure()
newax = plt.axes()
fig.add_subplot(newax)
newax.set_axis_bgcolor('none')
ax = plt.axes()
fig.add_subplot(ax)
ax.plot(x,y)

This is surprising because I thought the background of one would be overlaid on the other, but in either case it is the newax background that appears to be visible (or at least this is the color I see).

What is going on here?

  • 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-05-26T04:55:47+00:00Added an answer on May 26, 2026 at 4:55 am

    You’re not actually adding a new axes.

    Matplotlib is detecting that there’s already a plot in that position and returning it instead of a new axes object.

    (Check it for yourself. ax and newax will be the same object.)

    There’s probably not a reason why you’d want to, but here’s how you’d do it.

    (Also, don’t call newax = plt.axes() and then call fig.add_subplot(newax) You’re doing the same thing twice.)

    Edit: With newer (>=1.2, I think?) versions of matplotlib, you can accomplish the same thing as the example below by using the label kwarg to fig.add_subplot. E.g. newax = fig.add_subplot(111, label='some unique string')

    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    
    # If you just call `plt.axes()` or equivalently `fig.add_subplot()` matplotlib  
    # will just return `ax` again. It _won't_ create a new axis unless we
    # call fig.add_axes() or reset fig._seen
    newax = fig.add_axes(ax.get_position(), frameon=False)
    
    ax.plot(range(10), 'r-')
    newax.plot(range(50), 'g-')
    newax.axis('equal')
    
    plt.show()
    

    enter image description here

    Of course, this looks awful, but it’s what you’re asking for…

    I’m guessing from your earlier questions that you just want to add a second x-axis? If so, this is a completely different thing.

    If you want the y-axes linked, then do something like this (somewhat verbose…):

    import matplotlib.pyplot as plt
    
    fig, ax = plt.subplots()
    newax = ax.twiny()
    
    # Make some room at the bottom
    fig.subplots_adjust(bottom=0.20)
    
    # I'm guessing you want them both on the bottom...
    newax.set_frame_on(True)
    newax.patch.set_visible(False)
    newax.xaxis.set_ticks_position('bottom')
    newax.xaxis.set_label_position('bottom')
    newax.spines['bottom'].set_position(('outward', 40))
    
    ax.plot(range(10), 'r-')
    newax.plot(range(21), 'g-')
    
    ax.set_xlabel('Red Thing')
    newax.set_xlabel('Green Thing')
    
    plt.show()
    

    enter image description here

    If you want to have a hidden, unlinked y-axis, and an entirely new x-axis, then you’d do something like this:

    import matplotlib.pyplot as plt
    import numpy as np
    
    fig, ax = plt.subplots()
    fig.subplots_adjust(bottom=0.2)
    
    newax = fig.add_axes(ax.get_position())
    newax.patch.set_visible(False)
    newax.yaxis.set_visible(False)
    
    for spinename, spine in newax.spines.iteritems():
        if spinename != 'bottom':
            spine.set_visible(False)
    
    newax.spines['bottom'].set_position(('outward', 25))
    
    ax.plot(range(10), 'r-')
    
    x = np.linspace(0, 6*np.pi)
    newax.plot(x, 0.001 * np.cos(x), 'g-')
    
    plt.show()
    

    enter image description here

    Note that the y-axis values for anything plotted on newax are never shown.

    If you wanted, you could even take this one step further, and have independent x and y axes (I’m not quite sure what the point of it would be, but it looks neat…):

    import matplotlib.pyplot as plt
    import numpy as np
    
    fig, ax = plt.subplots()
    fig.subplots_adjust(bottom=0.2, right=0.85)
    
    newax = fig.add_axes(ax.get_position())
    newax.patch.set_visible(False)
    
    newax.yaxis.set_label_position('right')
    newax.yaxis.set_ticks_position('right')
    
    newax.spines['bottom'].set_position(('outward', 35))
    
    ax.plot(range(10), 'r-')
    ax.set_xlabel('Red X-axis', color='red')
    ax.set_ylabel('Red Y-axis', color='red')
    
    x = np.linspace(0, 6*np.pi)
    newax.plot(x, 0.001 * np.cos(x), 'g-')
    
    newax.set_xlabel('Green X-axis', color='green')
    newax.set_ylabel('Green Y-axis', color='green')
    
    
    plt.show()
    

    enter image description here

    You can also just add an extra spine at the bottom of the plot. Sometimes this is easier, especially if you don’t want ticks or numerical things along it. Not to plug one of my own answers too much, but there’s an example of that here: How do I plot multiple X or Y axes in matplotlib?

    As one last thing, be sure to look at the parasite axes examples if you want to have the different x and y axes linked through a specific transformation.

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

Sidebar

Related Questions

I'm trying to define a Data Driven Subscription for a report in SSRS 2005.
What is the best approach to define additional data for typedef enums in C?
I need to define new UI Elements as well as data binding in code
Is it possible to define an array of text fields (or any data type)
Is there a concise way to define properties in a ViewModel for data binding
I can easily define a datatype for a node of a directed graph. data
I'm creating test data for a Rails app. How do I define the value
given a string: msg=hello world How can I define this as a ctypes.c_void_p() data
I define my data model using Fluent nHibernate POCO classes + mappings. I'm also
I would like to use postgresql with foreign keys to define relationships in data

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.