Here are a few thoughts concerning margins management in a matplotlib chart. As you can see on the left chart, expanding the margins of your plot might be necessary to make the axis labels fully readable. You can easily fix it using the subplots_adjust() function. Note that this function can be used to expand the bottom margin or the top margin, depending where you need more space.

# Libraries
import matplotlib.pyplot as plt
import numpy as np

# Let's consider a basic barplot.
# Data
bars = ('A','B','C','D','E')
height = [3, 12, 5, 18, 45]
y_pos = np.arange(len(bars))
# Plot, height)
# If we have long labels, we cannot see it properly
names = ("very long group name 1","very long group name 2","very long group name 3","very long group name 4","very long group name 5")
plt.xticks(y_pos, names, rotation=90)
# Thus we have to give more margin:

# Show the graph
# It's the same concept if you need more space for your titles

# Plot, height)
# Title
plt.title("This is\na very very\nloooooong\ntitle!")
# Set margin
# Show the graph
Animation with python


Contact & Edit

👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥

This page is just a jupyter notebook, you can edit it here. Please help me making this website better 🙏!