close

How to change the X axis range in seaborn in python?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to change the X axis range in seaborn in python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to change the X axis range in seaborn in python?

  1. How to change the X axis range in seaborn in python?

    For the most flexible control with these kind of plots, create your own axes object then add the seaborn plots to it. Then you can perform the standard matplotlib changes to features like the x-axis, or use any of the normal controls available through the matplotlib API.

  2. change the X axis range in seaborn in python

    For the most flexible control with these kind of plots, create your own axes object then add the seaborn plots to it. Then you can perform the standard matplotlib changes to features like the x-axis, or use any of the normal controls available through the matplotlib API.

Method 1

For the most flexible control with these kind of plots, create your own axes object then add the seaborn plots to it. Then you can perform the standard matplotlib changes to features like the x-axis, or use any of the normal controls available through the matplotlib API.

Tested in python 3.8.12matplotlib 3.4.3seaborn 0.11.2

import matplotlib.pyplot as plt
import seaborn as sns

data = [5,8,12,18,19,19.9,20.1,21,24,28] 

fig, ax = plt.subplots()
sns.histplot(data, ax=ax)  # distplot is deprecate and replaced by histplot
ax.set_xlim(1,31)
ax.set_xticks(range(1,32))
plt.show()

With the ax and fig object exposed, you can edit the charts to your heart’s content now, and easily do stuff like changing the size with fig.set_size_inches(10,8))!

enter image description here

Method 2

I do not know if this is what you are looking for but I believe so:

import matplotlib.pyplot as plt
import seaborn as sns
tips = sns.load_dataset("tips")
sns.set_style("whitegrid")
g = sns.lmplot(x="tip", y="total_bill", data=tips,
 aspect=2)
g = (g.set_axis_labels("Tip","Total bill(USD)").
set(xlim=(0,15),ylim=(0,100)))
plt.title("title")
plt.show(g)

As you can see, the key part is the xlim=(0,15) where you specify the range you want to have. In your case:

xlim=(1,30)

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read