Understanding Why matplotlib show() Doesn't Work on Second Call in Jupyter Notebook

preview_player
Показать описание
Learn how to properly use `matplotlib` in Jupyter Notebook to display plots without issues on multiple calls.
---

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: matplotlib show() doesnt show() in second call

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Troubleshooting matplotlib show() in Jupyter Notebook

The Problem

Example Code Snippet

Cell 1: Creates the initial scatter plot.

[[See Video to Reveal this Text or Code Snippet]]

Cell 2: Attempts to add a new point and show the updated plot.

[[See Video to Reveal this Text or Code Snippet]]

When you run the second cell, you might expect to see the updated plot reflected in your notebook. Instead, you may only see text output indicating that a PathCollection object is created without any visuals.

The Solution: Reusing the Existing Figure

Updated Code Snippet for Cell 2

Here's how to adjust your second cell:

[[See Video to Reveal this Text or Code Snippet]]

Why Does This Work?

In Jupyter Notebook, when you execute a cell that contains a variable (like fig), it automatically returns the figure when you run that cell. Thus, by referencing fig directly, you ensure that the notebook displays the current state of the figure, including any new points added in that cell.

Conclusion

In summary, when working with matplotlib in Jupyter Notebooks:

Always create your plots in one cell and use the figure and axes objects (fig and ax) to modify the plot in subsequent cells.

By following these guidelines, you can easily manage and display your plots, making your data visualization tasks much more efficient and visually appealing!

If you have any further questions or need additional clarification on this topic, feel free to ask in the comments below!
Рекомендации по теме
welcome to shbcf.ru