dbuskirk / CourseFiles

0 stars 0 forks source link

Interactive Matplotlib #1

Open ianhi opened 3 years ago

ianhi commented 3 years ago

Hi,

I saw your interactive plots notebook and wanted to note a few things about how interactive matplotlib in the notebook works.

( I love that you will be teaching students about interactive matplotlib in the notebook!)

Curious "undocumented feature"

The code that generates the interactive plot cannot be run twice. The second run will not generate a plot. However, if you re-run the widge magic before the second run, then an interactive plot will be generated.

Pyplot works on the idea of a current active figure, which is global state, and plt..... commands work on this figure. So those commands are running properly when you run the cell a second time, but the issue is that the figure is no longer displayed. The first time you run the cell after calling %matplotlib widget there are no existing figures, so a figure is created and displayed automatically. When you set the backend again you are reseting the backend initialization and destroying all existing figures.

You can see this if you run the same code from a ipython prompt., the plot that pops up will continue to receive the results of new plotting commands.

The normal way to deal with this is to make a new figure for each plot you are making. You can do this either via fig = plt.figure() or fig, ax = plt.subplots(). So for your examples instead of calling %matplotlib widget you want a new figure I would call plt.figure()

fig = plt.figure()
plt.scatter("total_bill", "tip",data=dfTips)
plt.xlabel("Total Bill")
plt.ylabel("Tip")

You can then also display the same figure in multiple places using display(fig.canvas) for an interactive plot, or display(fig) to embed a static png.

A lot of this is explained in the (somewhat hard to find) ipympl example notebook: https://github.com/matplotlib/ipympl/blob/master/examples/ipympl.ipynb

we don't need, and, in fact cannot invoke, ipympl, because we have already invoked %matplotlib widget

Somewhat confusingly ipympl and widget are aliases for the same thing either one is fine.


All the best! Ian

dbuskirk commented 3 years ago

Thank you very much for your helpful and informative comments.


From: Ian Hunt-Isaak @.> Sent: Monday, June 28, 2021 2:35 PM To: dbuskirk/CourseFiles @.> Cc: Subscribed @.***> Subject: [dbuskirk/CourseFiles] Interactive Matplotlib (#1)

Hi,

I saw your interactive plots notebook and wanted to note a few things about how interactive matplotlib in the notebook works.

( I love that you will be teaching students about interactive matplotlib in the notebook!)

Curious "undocumented feature"

The code that generates the interactive plot cannot be run twice. The second run will not generate a plot. However, if you re-run the widge magic before the second run, then an interactive plot will be generated.

Pyplot works on the idea of a current active figure, which is global state, and plt..... commands work on this figure. So those commands are running properly when you run the cell a second time, but the issue is that the figure is no longer displayed. The first time you run the cell after calling %matplotlib widget there are no existing figures, so a figure is created and displayed automatically. When you set the backend again you are reseting the backend initialization and destroying all existing figures.

You can see this if you run the same code from a ipython prompt., the plot that pops up will continue to receive the results of new plotting commands.

The normal way to deal with this is to make a new figure for each plot you are making. You can do this either via fig = plt.figure() or fig, ax = plt.subplots(). So for your examples instead of calling %matplotlib widget you want a new figure I would call plt.figure()

fig = plt.figure() plt.scatter("total_bill", "tip",data=dfTips) plt.xlabel("Total Bill") plt.ylabel("Tip")

You can then also display the same figure in multiple places using display(fig.canvas) for an interactive plot, or display(fig) to embed a static png.

A lot of this is explained in the (somewhat hard to find) ipympl example notebook: https://github.com/matplotlib/ipympl/blob/master/examples/ipympl.ipynb

we don't need, and, in fact cannot invoke, ipympl, because we have already invoked %matplotlib widget

Somewhat confusingly ipympl and widget are aliases for the same thing either one is fine.


All the best! Ian

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/dbuskirk/CourseFiles/issues/1, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAVZTOA7BSHPPBXXKSAK42DTVC6FVANCNFSM47OP5VVA.