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

  • Home
  • SEARCH
  • 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 7932649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:04:47+00:00 2026-06-03T21:04:47+00:00

This is based on generating a second x-axis, as described in this previous post:

  • 0

This is based on generating a second x-axis, as described in this previous post:

matplotlib: adding second axes() with transparent background?

Included below is code to generate a plot with two x-axis that represent two different time units for the same data: relative time (rel_time) and an absolute time (abs_time). While the method described above nicely generates the two axes, the data is scaled differently between them.

How can I generate a plot with two x-axes that are linked? That is, the data plotted on both each axis should be aligned with the other.

Furthermore, is there a way to do this without plotting the data (y) twice? In the end the data should be aligned, so the second plot is just there to generate a new axis.

The code below:

# Based on question:
# https://stackoverflow.com/questions/7761778/matplotlib-adding-second-axes-with-transparent-background
import time
import numpy as np
import matplotlib.pyplot as plt

# Plot Data
rel_time = np.arange(-10.0, 40)  # seconds
abs_time = rel_time + time.time()  # epoch time
y = np.array([1.1, 1.2, 1.2, 1.1, 1.2, 1.1, 1.3, 1.3, 1.2, 1.1, 1.4, 1.7, 
              2.5, 2.6, 3.5, 4, 4.3, 4.8, 5, 4.9, 5.3, 5.2, 5.5, 5.1, 
              5.4, 5.6, 5.1, 6, 6.2, 6.2, 5.5, 6.1, 5.4, 6.3, 6.2, 6.5,
              6.3, 6.1, 6.5, 6.6, 6.1, 6.6, 6.5, 6.4, 6.6, 6.5, 6.2, 6.6,
              6.4, 6.8])  # Arbitrary data

fig = plt.figure()
fig.subplots_adjust(bottom=0.25)  # space on the bottom for second time axis

host = fig.add_subplot(111)  # setup plot

p1 = host.plot(rel_time, y, 'b-')  # plot data vs relative time
host.set_xlabel("Relative Time [sec]")
host.set_ylabel("DATA")

newax = host.twiny()  # create new axis
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', 50))
newax.plot(abs_time, y, 'k-')  # plot data vs relative time
newax.set_xlabel("Absolute Time [Epoch sec]")

plt.show()

generates a timeseries plot with mismatched x-axes:
Timeseries Plot with mismatched x-axes that display different time units

  • 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-03T21:04:49+00:00Added an answer on June 3, 2026 at 9:04 pm

    You need to specify limits for your x-axes. If you add these lines it will do it:

    host.set_xlim(rel_time[0],rel_time[-1])
    newax.set_xlim(abs_time[0],abs_time[-1])
    

    both curves plotted:

    enter image description here

    If you don’t want to draw the data twice, just delete the newax.plot()line.

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

Sidebar

Related Questions

I was facing this unique problem of generating a bit-mask based on the input
I'm generating pages based on an sql query. This is the query: CREATEPROCEDURE sp_searchUsersByFirstLetter
I am generating the sql statement below based on some coldfusion logic, but it
I posted a previous question about generating a bezier curve based on only the
I am currently trying to achieve something like this: Based on this class, I
This is based on a similar question How to Replace Multiple Characters in Access
This is based on my full question . I decided to take it in
Some time ago I created this LAMP based web. At the time I wrote
I am designing a simple web-based application. I am new to this web-based domain.I
This question is based on the thread . I have the shell function function

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.