98

From the official seaborn documentation, I learned that you can create a boxplot as below:

import seaborn as sns
sns.set_style("whitegrid")
tips = sns.load_dataset("tips")
ax = sns.boxplot(x="day", y="total_bill", data=tips)

Seaborn Boxplot Example

My question is: how do I limit the range of y-axis of this plot? For example, I want the y-axis to be within [10, 40]. Is there any easy way to do this?

1 Answer 1

151

It is standard matplotlib.pyplot:

...
import matplotlib.pyplot as plt
plt.ylim(10, 40)

Or simpler, as mwaskom comments below:

ax.set(ylim=(10, 40))

enter image description here

6
  • 37
    Or, better, ax.set(ylim=(10, 40))
    – mwaskom
    Oct 24, 2015 at 1:59
  • 2
    Why is this better, @mwaskom?
    – MERose
    Jun 14, 2017 at 17:41
  • 5
    It is likely that mwaskom prefers the object-oriented side of matplotlib (calling methods on objects) rather than the more procedural approach that matplotlib.pyplot takes (calling global functions in a particular order).
    – NasaGeek
    Jul 13, 2017 at 20:25
  • If my y-axis is datetime values, how do I write the values in the format given by @mwaskom? Writing dates give me invalid token error.
    – deadcode
    Jan 9, 2018 at 18:32
  • 3
    @MERose I don't think 'plt.ylim(10, 40)' works on the new seaborn plots (relplot, catplot). There is a couple others too. 'ax.set(ylim=(10, 40))' solves for this.
    – bbennett36
    Mar 14, 2019 at 1:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.