dvgodoy / PyTorchStepByStep

Official repository of my book: "Deep Learning with PyTorch Step-by-Step: A Beginner's Guide"
https://pytorchstepbystep.com
MIT License
866 stars 332 forks source link

A simple question about how you are using matplotlib? #9

Closed minertom closed 3 years ago

minertom commented 3 years ago

Hi, and i realize that this may be trivial.

In your "figures" you are using python functions that are defined in the "plots" directory.

In the jupyter notebook you use figure1(x_train, y_train, x_val, y_val

This calls the function `def figure1(x_train, y_train, x_val, y_val): fig, ax = plt.subplots(1, 2, figsize=(12, 6))

ax[0].scatter(x_train, y_train)
ax[0].set_xlabel('x')
ax[0].set_ylabel('y')
ax[0].set_ylim([0, 3.1])
ax[0].set_title('Generated Data - Train')

ax[1].scatter(x_val, y_val, c='r')
ax[1].set_xlabel('x')
ax[1].set_ylabel('y')
ax[1].set_ylim([0, 3.1])
ax[1].set_title('Generated Data - Validation')
fig.tight_layout()

return fig, ax

` I notice that there is no plt.show() statement that is needed, either in the function or in the jupyter notebook.

However, if I am at the command line (not using the jupyter notebook) and I enter the python code line by line and make the "figure" call, I don't get a plot unless I use "plt.show()" as the next line after the figure call.

Can you tell me why plt.show() is not necessary for the jupyter notebook but it is necessary when calling the function from the command line?

Thank You Tom

dvgodoy commented 3 years ago

Hi Tom,

I am assuming you figured this out by now since you already closed the issue :-) But, answering your question, the key is in Jupyter notebook's "magic" command: %matplotlib inline, which captures the figures generated by matplotlib and displays them inside the notebook.

Anyway, if you want to learn a bit more about matplotlib's display options, this material from Jake VanderPlas is really good: https://jakevdp.github.io/PythonDataScienceHandbook/04.00-introduction-to-matplotlib.html

Best, Daniel

minertom commented 3 years ago

Hi and I do appreciate your help.

I closed the issue because I found that matplotlib can be used in interactive mode, non interactive mode and with "backends", of which, jupyter, is one of them. There is a "block" involved, which just says that if one is returning from a python function, like you do in figure1, figure2...etc, there is no block and plt.show() is not required.

From the matlab documentation: https://www.google.com/search?channel=fs&client=ubuntu&q=matplotlib+when+is+plt.show%28%29+needed

Also, I checked in chapter0, you never did use "%matplotlib inline " but at least now, I am closer to understanding what it does.

Thank You Tom

On Mon, Nov 23, 2020 at 1:00 PM Daniel Voigt Godoy notifications@github.com wrote:

Hi Tom,

I am assuming you figured this out by now since you already closed the issue :-) But, answering your question, the key is in Jupyter notebook's "magic" command: %matplotlib inline, which captures the figures generated by matplotlib and displays them inside the notebook.

Anyway, if you want to learn a bit more about matplotlib's display options, this material from Jake VanderPlas is really good:

https://jakevdp.github.io/PythonDataScienceHandbook/04.00-introduction-to-matplotlib.html

Best, Daniel

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/dvgodoy/PyTorchStepByStep/issues/9#issuecomment-732423150, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADHGGHFESQARO6DLCZYFIODSRLEPTANCNFSM4T7AJ46Q .