5. Inline Media & Rich Outputs
Inline Plotting with Magic Commands
Jupyter Notebooks provide 'magic commands' to extend functionality. To ensure your plots appear directly below your code cells, use the '%matplotlib inline' magic command. This ensures that the matplotlib library renders its output as a static image in the notebook interface rather than opening an external GUI window.
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6]) plt.title('Basic Inline Plot') plt.show()
Rendering Tabular Data
When working with pandas, data is stored in DataFrame objects. Jupyter is designed to automatically detect these objects and render them as clean, sortable HTML tables instead of plain text output. This makes inspecting large datasets much easier and visually intuitive compared to traditional console output.
IPython.display to force-render a dataframe even if it is not the last line of a cell.
import pandas as pd df = pd.DataFrame({'Team': ['VoidX', 'GhostOps'], 'Score': [95, 88]}) display(df)
Advanced Media with IPython Display
The IPython.display module provides specialized classes like Image, Audio, and YouTubeVideo to embed rich media directly. By passing a file path or URL to these constructors, you can integrate external multimedia assets into your documentation workflow, transforming your notebook into an interactive presentation.
IPython.display is preferred over raw HTML strings because it provides better integration with the notebook's internal state handling.
from IPython.display import Image # Displays a remote image Image(url='https://www.python.org/static/community_logos/python-logo.png')
Knowledge Check
Ready to test your understanding of 5. Inline Media & Rich Outputs?