1 Hour

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.

Operator Note: Always run your matplotlib magic command in the first cell of your notebook to ensure all subsequent plots render correctly.
Python 3
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.

Operator Note: You can use the 'display()' function from IPython.display to force-render a dataframe even if it is not the last line of a cell.
Python 3
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.

Operator Note: Using IPython.display is preferred over raw HTML strings because it provides better integration with the notebook's internal state handling.
Python 3
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?

Back