Is there a way to go back and forth between Y-axis plots while matplotlib is actively running?

I created a plot using the S&P 500 historical closing data on 1 Y-axis over the Russell 2000 data on another Y-axis with a start and end date range as the X-axis. You can see an example here:

I’d like to switch back and forth between the Russell 2000 as a Y-axis and then the Yield Curves over the S&P 500. What if I have even more data points I want to plot on top of the S&P 500? Is there a way to do this using matplotlib or would I have to use an external GUI library like Tkinter? Thanks.

Do radio buttons help where you just toggle the visibility? I don’t think you need an external GUI for something this straight forward.

That’s a good suggestion. I think it will and it’s the easiest solution too. Thanks.

It turns out I don’t know how to really use radio buttons like how I wanted to.

Here’s the full code and question I posted on Stack:

Any advice?

I’d start by writing something simpler, and then working my way up. Your stack overflow question is pretty hard to parse.

I suggest starting with the example @jklymak pointed to

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons

t = np.arange(0.0, 2.0, 0.01)
s0 = np.sin(2*np.pi*t)
s1 = np.sin(4*np.pi*t)

lines = {}

fig, ax = plt.subplots()
lines['A'],  = ax.plot(t, s0, lw=2, color='red', visible=False)
lines['B'],  = ax.plot(t, s1, lw=2, color='blue', visible=False)

ax.set_xlim(0, 2)
ax.set_ylim(-1, 1)

axcolor = 'lightgoldenrodyellow'
rax = plt.axes([0.05, 0.7, 0.15, 0.15], facecolor=axcolor)
radio = RadioButtons(rax, ('A', 'B'))

def toggle(label):
    for ln in lines.values():

    ax.set_title(f"currently showing: {label}")

which will toggle between the two sine waves when you click on the radio button.

In general, examples that we can copy-paste-run make it much easier for us to help you.

Your code example worked. I can now switch between two Y-axis plots; however, corresponding X-axis doesn’t update. Here’s my current code.

today = str(

sp_symbol = ‘^GSPC’
spfile = "C:/Users/Notebook/Desktop/Data/S&P 500 " + today + “.csv”
sp_df = pd.read_csv(spfile)
sp_df[‘Date’] = pd.to_datetime(sp_df[‘Date’])
sp_df = sp_df.sort_values(‘Date’, ascending=True)

russ_symbol = ‘^RUT’
russfile = "C:/Users/Notebook/Desktop/Data/Russell 2000 " + today + “.csv”
rus_df = pd.read_csv(russfile)
rus_df[‘Date’] = pd.to_datetime(rus_df[‘Date’])
rus_df = rus_df.sort_values(‘Date’, ascending=True)

inv_yld_crv_symbol = “Yield Curve”
iyc_file = "C:/Users/Notebook/Desktop/Data/Treasury Yield Curve " + today + “.csv”
inv_yld_crv_df = pd.read_csv(iyc_file)
inv_yld_crv_df[‘Date’] = pd.to_datetime(inv_yld_crv_df[‘Date’])
inv_yld_crv_df = inv_yld_crv_df.sort_values(‘Date’, ascending=True)
inv_yld_crv_df[‘Diff_10Y_Minus_3M’] = inv_yld_crv_df[‘10 YR’] - inv_yld_crv_df[‘3 MO’]

start_date = ‘1990-01-01’
end_date = ‘2022-01-01’
sp_df = sp_df[(sp_df[‘Date’] >= start_date) & (sp_df[‘Date’] < end_date)]
rus_df = rus_df[(rus_df[‘Date’] >= start_date) & (rus_df[‘Date’] < end_date)]

lines = {}

fig, ax = plt.subplots(figsize=(15, 7))

ax.plot(sp_df[‘Date’], sp_df[‘Close’], color=‘red’, label=sp_symbol)
ax.legend(loc=‘upper left’)

This creates the two different Y-axis plots that I switch between.

ax2 = ax.twinx()
lines[‘Russell 2000’], = ax2.plot(rus_df[‘Date’], rus_df[‘Close’], color=‘blue’, label=russ_symbol)
lines[‘Yield Curve’], = ax2.plot(inv_yld_crv_df[‘Date’], inv_yld_crv_df[‘Diff_10Y_Minus_3M’], color=‘green’, label=inv_yld_crv_symbol)

axcolor = ‘lightgoldenrodyellow’
rax = plt.axes([0.05, 0.7, 0.10, 0.10], facecolor=axcolor)
radio = RadioButtons(rax, (‘Russell 2000’, ‘Yield Curve’))

def toggle(label):
for ln in lines.values():

This works, but the corresponding X-axis doesn’t change with it. Is there something I have to do to change X-axis as well?

ax.set_title("S&P 500 vs. " + label)
ax2.legend(loc="lower right")

toggle(‘Russell 2000’)