Categories
Python Answers

How to change the x axis in matplotlib so there is no white space with Python?

Spread the love

Sometimes, we want to change the x axis in matplotlib so there is no white space with Python.

In this article, we’ll look at how to change the x axis in matplotlib so there is no white space with Python.

How to change the x axis in matplotlib so there is no white space with Python?

To change the x axis in matplotlib so there is no white space with Python, we can call the margins method.

For instance, we write

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset('tips')

fig, (ax) = plt.subplots(1, 1, figsize=(10, 4))
tips.plot(ax=ax, title='no margins')
ax.margins(x=0)

to call subplots to create the subplots.

Then we call plot with the ax plot to plot it.

Next, we call ax.margins with the x argument set to 0 to remove the margins from the x-axis.

Conclusion

To change the x axis in matplotlib so there is no white space with Python, we can call the margins method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *