#192 About Matplotlib margins

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 can be necessary to make the axis labels fully readable. This is easy fix 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.

# Let's consider a basic barplot.
import matplotlib.pyplot as plt
import numpy as np
y_pos = np.arange(len(bars))
bars = ('A','B','C','D','E')
height = [3, 12, 5, 18, 45]
plt.bar(y_pos, 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:

# It's the same concept if you need more space for your titles
plt.title("This is\na very very\nloooooong\ntitle!")

  • Sponsors

  • Leave a Reply

    Your email address will not be published.