Python Forum

Full Version: Why the NameError?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

In Jupyter Notebook I ran these cells:

sample_data = [[3000,18], [3200,17], [3500,16], [4000,12]]
sample_df = pd.DataFrame(sample_data, columns = ['SPX', 'Spread_Price'])
Next, I ran this as a single cell:

fig, axs = plt.subplots()
axs.plot(sample_df['SPX'])
axs.twinx()
axs.plot(sample_df['Spread_Price'])
plt.show()
This gave me a graph. Before I get into what's wrong with the graph, I want to point out that it had no problem processing the df still in memory from the cell just ran before. This is how JN usually works and one reason I really like it.

However, earlier this morning I ran my full 350-line program before pasting the last 55 lines into a separate file. This is the portion of the program responsible for the graphing. I got NameError. First, plt was not defined even though running the full program included "import matplotlib.pyplot as plt". After adding that on top of the separate file, I got btstats---name of the dataframe defined in the full program--not defined. Any ideas as to why these names might not have been retrievable from memory since I had run them previously in this JN session (e.g. I didn't shut down the kernel, close the tab, or otherwise reset JN)?
Wonder if a disconnect occurred even though you had not shut down, due to the time elapsed. I always put a separate cell for the imports, and then separate cells for each set of tasks. When I get that error I just start back at the top, do the imports, and continue to process down. I would not expect the kernal to have survived overnight.
I'm still able to insert a cell below now and print btstats so it's still in memory. Maybe there's something about my syntax. I'm all confused about the fig/ax notation.

I have figured some stuff out here, though, that answers a few questions I was going to ask next. I'll post that below.
The problem with the code above is .twinx() needs to be an attribute of ax and plot is a method (I think I have the terminology correct) of that.

This works:

fig, axs = plt.subplots(1,2)
axs[0].plot(sample_df['Spread_Price'],color='red')
axs[0].twinx().plot(sample_df['SPX']) 
axs[1].twinx().plot(sample_df['Spread_Price']) 
axs[1].plot(sample_df['SPX'],color='red')
plt.tight_layout()
plt.show()
If I want to switch around primary and secondary axis, then I just need to switch which Series follows .twinx().

If I figure out what's causing the NameError then I'll report back. It's not an issue now, though, since I have figured out this syntax (I can therefore run the whole program).
(May-16-2022, 05:09 PM)Mark17 Wrote: [ -> ]The problem with the code above is .twinx() needs to be an attribute of ax and plot is a method (I think I have the terminology correct) of that.

This works:

fig, axs = plt.subplots(1,2)
axs[0].plot(sample_df['Spread_Price'],color='red')
axs[0].twinx().plot(sample_df['SPX']) 
axs[1].twinx().plot(sample_df['Spread_Price']) 
axs[1].plot(sample_df['SPX'],color='red')
plt.tight_layout()
plt.show()
If I want to switch around primary and secondary axis, then I just need to switch which Series follows .twinx().

If I figure out what's causing the NameError then I'll report back. It's not an issue now, though, since I have figured out this syntax (I can therefore run the whole program).

For the record, after a more recent thread I now realize .twinx() is not an attribute but a function. It may be more appropriate to say L3-L4 involve chained functions: the additional axis is added and then the plot is done.

When I went ahead and tried to label the axis with .twinx() again, I realized I hadn't figured this out correctly (as that resulted in a second set of overlapping axis labels).