spedas / pyspedas

Python-based Space Physics Environment Data Analysis Software
https://pyspedas.readthedocs.io/
MIT License
143 stars 58 forks source link

Adding Secondary axis on the top of the figure. #881

Open pspathare opened 2 weeks ago

pspathare commented 2 weeks ago

I am using tplot() to plot solar wind speed measured by SPC and SPI. In this plot I want to add another axis which is indicating the spacecraft distance from the sun on the upper side of the figure.

For example, I am posting a random plot that I obtained using matplotlib showing what I mean by having an axis on the upper side of the plot. Figure_5

I think there could be some option in pytplot.options() that I can use to get a secondary axis on the top of my figure. Eg. border(Turns on or off the top/right axes that would create a box around the plot.) or plotter(Allows a user to implement their own plotting script in place of the ones herein.) But I don’t know how to use these options. I am not completely sure if I could use these options or not. I would really appreciate it if you could help me to plot the secondary axis on the top of the figure.

I have attached the remaining code for plotting solar wind speed of SPC and SPI


import pyspedas import pytplot import numpy as np import matplotlib.pyplot as plt from pytplot import tplot import matplotlib.pyplot as plt from pytplot import get_data import datetime

SPC Solar wind data

spc_vars = pyspedas.psp.spc(trange=['2020-09-24/00:00', '2020-09-30/23:59'], datatype='l3i', level='l3') Solar_wind_speed_data_spc = get_data('psp_spc_vp_moment_RTN') split_data = pytplot.tplot_math.split_vec('psp_spc_vp_moment_RTN') v_r = pytplot.data_quants['psp_spc_vp_moment_RTN_x'].values v_t= pytplot.data_quants['psp_spc_vp_moment_RTN_y'].values v_n = pytplot.data_quants['psp_spc_vp_moment_RTN_z'].values v_mag_spc_val= np.sqrt(v_r2 + v_t2 + v_n**2) time_sw_spc = Solar_wind_speed_data_spc.times datetimes_spc =[datetime.datetime.utcfromtimestamp(ts) for ts in time_sw_spc] pytplot.store_data('v_mag_spc',data = {'x' :datetimes_spc, 'y': v_mag_spc_val}) val = pytplot.data_quants['v_mag_spc'].values pytplot.tplot_math.avg_res_data('v_mag_spc', 60,'v_mag_spc_avg')

SPI Solar wind data

spi_vars = pyspedas.psp.spi(trange=['2020-09-24/00:00', '2020-09-30/23:59'], datatype='sf00_l3_mom', level='l3', time_clip=True) Solar_wind_speed_data = get_data('psp_spi_VEL_RTN_SUN') time_sw_spi = Solar_wind_speed_data.times datetimes_spi = [datetime.datetime.utcfromtimestamp(ts) for ts in time_sw_spi] split_data_spi = pytplot.tplot_math.split_vec('psp_spi_VEL_RTN_SUN') v_r_spi =pytplot.data_quants['psp_spi_VEL_RTN_SUN_x'].values v_t_spi=pytplot.data_quants['psp_spi_VEL_RTN_SUN_y'].values v_n_spi=pytplot.data_quants['psp_spi_VEL_RTN_SUN_z'].values v_mag_spi_val= np.sqrt(v_r_spi2 + v_t_spi2 + v_n_spi**2) pytplot.store_data('v_mag_spi',data = {'x' :datetimes_spi, 'y': v_mag_spi_val}) pytplot.tplot_math.avg_res_data('v_mag_spi', 60,'v_mag_spi_avg')

Spacecraft distance data

distance_data = get_data('psp_spc_sc_pos_HCI') time_d= distance_data.times split_data_distance = pytplot.tplot_math.split_vec('psp_spc_sc_pos_HCI') R_r= pytplot.data_quants['psp_spc_sc_pos_HCI_x'].values R_t=pytplot.data_quants['psp_spc_sc_pos_HCI_y'].values R_n=pytplot.data_quants['psp_spc_sc_pos_HCI_z'].values R_mag = np.sqrt(R_r2 + R_t2 + R_n**2) pytplot.store_data('R_distance',data={'x':time_d,'y':R_mag})

pytplot.options('v_mag_spc_avg', opt_dict ={'Color':'Green', 'legend_names':['v_mag_spc'],'border':True}) pytplot.options('v_mag_spi_avg', opt_dict ={'Color':'Red', 'legend_names':['v_mag_spi'],'border':True})

pytplot.options('V_A_vs_V_SW_spi','ytitle','Speed') pytplot.store_data('V_A_vs_V_SW_SPC_SPI',data=['v_mag_spc_avg','v_mag_spi_avg']) pytplot.options('V_A_vs_V_SW_SPC_SPI','ytitle','speed')

tplot('V_A_vs_V_SW_SPC_SPI')

jameswilburlewis commented 2 weeks ago

Thanks for bringing this up! One option that might work for you right now is to use return_plot_objects=True in your call to tplot(), and use the returned fig and axes objects to manipulate the plots directly with matplotlib calls.

But it would also be good to be able to enable features like this, or set custom plotters, via pytplot.options(). We're going to make another pass over the pytplot.options() and pytplot.tplot_options() in the near future to implement some of the missing features, so we'll try to include some additional axis placement options.