To make a Matplotlib figure span the entire width of a Jupyter Notebook cell, you can use the following approach:
Use the figsize parameter in plt.figure() or directly in plotting functions like plt.subplots() to set the width and height of the figure. For example:
pythonimport matplotlib.pyplot as plt plt.figure(figsize=(15, 5)) # Width: 15 inches, Height: 5 inches # Your plotting code here plt.show()
Jupyter Notebook supports a magic command to automatically adjust the figure size to the cell width:
python%matplotlib inline %config InlineBackend.figure_format = 'retina' plt.rcParams['figure.figsize'] = [15, 5] # Set default figure size
If you want the figure to span the entire notebook width, you can also adjust the notebook's cell output width using custom CSS:
pythonfrom IPython.display import display, HTML display(HTML("<style>.container { width:100% !important; }</style>"))
Then, set the figure width to a large value (e.g., 20 inches):
pythonplt.figure(figsize=(20, 5)) # Your plotting code here plt.show()
tight_layoutTo ensure labels and titles fit within the figure, use tight_layout():
pythonplt.tight_layout()
Here’s a complete example:
pythonimport matplotlib.pyplot as plt # Set the figure size plt.figure(figsize=(20, 5)) # Plot something plt.plot([1, 2, 3, 4], [1, 4, 9, 16]) # Adjust layout plt.tight_layout() # Show the plot plt.show()
This will make the figure span the entire width of the Jupyter Notebook cell. Adjust the figsize values as needed for your specific use case!
Vibe can make mistakes. Check answers. Learn more