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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:33:58+00:00 2026-05-15T14:33:58+00:00

Trying to plot a spectrum, ie, velocity versus intensity, with lower x axis =

  • 0

Trying to plot a spectrum, ie, velocity versus intensity, with lower x axis = velocity, on the upper twin axis = frequency

The relationship between them (doppler formula) is

f = (1-v/c)*f_0 

where f is the resulting frequency, v the velocity, c the speed of light, and f_0 the frequency at v=0, ie. the v_lsr.

I have tried to solve it by looking at http://matplotlib.sourceforge.net/examples/axes_grid/parasite_simple2.html , where it is solved by

pm_to_kms = 1./206265.*2300*3.085e18/3.15e7/1.e5
aux_trans = matplotlib.transforms.Affine2D().scale(pm_to_kms, 1.)
ax_pm = ax_kms.twin(aux_trans)
ax_pm.set_viewlim_mode("transform")

my problem is, how do I replace the pm_to_kms with my function for frequency?

Anyone know how to solve this?

  • 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-15T14:33:59+00:00Added an answer on May 15, 2026 at 2:33 pm

    The solution I ended up using was:

    ax_hz = ax_kms.twiny()
    x_1, x_2 = ax_kms.get_xlim()
    # i want the frequency in GHz so, divide by 1e9
    ax_hz.set_xlim(calc_frequency(x_1,data.restfreq/1e9),calc_frequency(x_2,data.restfreq/1e9))
    

    This works perfect, and much less complicated solution.

    EDIT : Found a very fancy answer.
    EDIT2 : Changed the transform call according to the comment by @u55

    This basically involves defining our own conversion/transform. Because of the excellent AstroPy Units equivalencies, it becomes even easier to understand and more illustrative.

    from matplotlib import transforms as mtransforms
    import astropy.constants as co
    import astropy.units as un
    import numpy as np 
    import matplotlib.pyplot as plt 
    plt.style.use('ggplot')
    from mpl_toolkits.axes_grid.parasite_axes import SubplotHost 
    
    
    class Freq2WavelengthTransform(mtransforms.Transform): 
        input_dims = 1 
        output_dims = 1 
        is_separable = False 
        has_inverse = True 
    
        def __init__(self):
            mtransforms.Transform.__init__(self)
    
        def transform_non_affine(self, fr): 
            return (fr*un.GHz).to(un.mm, equivalencies=un.spectral()).value 
    
        def inverted(self): 
            return Wavelength2FreqTransform() 
    
    class Wavelength2FreqTransform(Freq2WavelengthTransform): 
        input_dims = 1 
        output_dims = 1 
        is_separable = False 
        has_inverse = True 
    
        def __init__(self):
            mtransforms.Transform.__init__(self)
    
        def transform_non_affine(self, wl): 
            return (wl*un.mm).to(un.GHz, equivalencies=un.spectral()).value 
    
        def inverted(self): 
            return Freq2WavelengthTransform() 
    
    
    
    aux_trans = mtransforms.BlendedGenericTransform(Wavelength2FreqTransform(), mtransforms.IdentityTransform()) 
    
    fig = plt.figure(2) 
    
    ax_GHz = SubplotHost(fig, 1,1,1) 
    fig.add_subplot(ax_GHz) 
    ax_GHz.set_xlabel("Frequency (GHz)") 
    
    
    xvals = np.arange(199.9, 999.9, 0.1) 
    
    # data, noise + Gaussian (spectral) lines
    data = np.random.randn(len(xvals))*0.01 + np.exp(-(xvals-300.)**2/100.)*0.5 + np.exp(-(xvals-600.)**2/400.)*0.5
    
    ax_mm = ax_GHz.twin(aux_trans) 
    ax_mm.set_xlabel('Wavelength (mm)') 
    ax_mm.set_viewlim_mode("transform") 
    ax_mm.axis["right"].toggle(ticklabels=False) 
    
    ax_GHz.plot(xvals, data) 
    ax_GHz.set_xlim(200, 1000) 
    
    plt.draw() 
    plt.show() 
    

    This now produces the desired results:
    enter image description here

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

Sidebar

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.