Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why the NameError?
#1
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)?
Reply
#2
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.
Mark17 likes this post
Reply
#3
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.
Reply
#4
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).
Reply
#5
(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).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  NameError: NameError: global name 'BPLInstruction' is not defined colt 7 4,420 Oct-27-2019, 07:49 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020