Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to set axes labels & limits in a Seaborn plot?
Seaborn automatically adjusts labels and axes limits to make plots more understandable, but sometimes you need custom control. Setting appropriate axes labels helps viewers understand what the plot represents, while adjusting limits lets you focus on specific data ranges. We can use matplotlib functions like xlabel(), ylabel(), xlim(), and ylim() to customize Seaborn plots.
Core Functions for Axes Customization
Here are the main functions used to set labels and limits:
plt.xlabel()? Sets the x-axis label textplt.ylabel()? Sets the y-axis label textplt.xlim()? Sets the x-axis range limitsplt.ylim()? Sets the y-axis range limits
Setting Labels and Limits in Scatter Plot
Here's how to customize a scatter plot with labels and limits:
import seaborn as sns
import matplotlib.pyplot as plt
# Sample data
x_values = [1, 2, 3, 4, 5]
y_values = [2, 4, 6, 8, 10]
# Create scatter plot
sns.scatterplot(x=x_values, y=y_values)
# Set custom labels and limits
plt.xlabel('Time (hours)')
plt.ylabel('Distance (km)')
plt.xlim(0, 6)
plt.ylim(0, 12)
plt.show()
Customizing Line Plot
Line plots work similarly with the same functions:
import seaborn as sns
import matplotlib.pyplot as plt
# Sample data
x_values = [1, 2, 3, 4, 5]
y_values = [3, 7, 4, 8, 5]
# Create line plot
sns.lineplot(x=x_values, y=y_values)
# Customize axes
plt.xlabel('Month')
plt.ylabel('Sales (thousands)')
plt.xlim(0, 6)
plt.ylim(0, 10)
plt.show()
Bar Plot with Custom Labels
For categorical data, you typically only need to set y-axis limits:
import seaborn as sns
import matplotlib.pyplot as plt
# Categorical data
categories = ['Product A', 'Product B', 'Product C', 'Product D']
values = [25, 40, 30, 45]
# Create bar plot
sns.barplot(x=categories, y=values)
# Set labels and y-limit
plt.xlabel('Products')
plt.ylabel('Revenue (thousands)')
plt.ylim(0, 50)
plt.show()
Histogram with Range Control
Histograms benefit from both axis customizations:
import seaborn as sns
import matplotlib.pyplot as plt
# Sample data
scores = [85, 90, 78, 92, 88, 76, 95, 89, 82, 91, 87, 84]
# Create histogram
sns.histplot(scores)
# Customize axes
plt.xlabel('Test Scores')
plt.ylabel('Frequency')
plt.xlim(70, 100)
plt.show()
Box Plot Customization
Box plots often need only label and y-limit adjustments:
import seaborn as sns
import matplotlib.pyplot as plt
# Sample data
temperatures = [22, 25, 28, 24, 26, 23, 27, 29, 25, 24, 26, 28]
# Create box plot
sns.boxplot(data=temperatures)
# Set labels and limits
plt.xlabel('Daily Temperature')
plt.ylabel('Temperature (°C)')
plt.ylim(20, 30)
plt.show()
Summary
| Function | Purpose | Example |
|---|---|---|
plt.xlabel() |
Set x-axis label | plt.xlabel('Time') |
plt.ylabel() |
Set y-axis label | plt.ylabel('Value') |
plt.xlim() |
Set x-axis range | plt.xlim(0, 10) |
plt.ylim() |
Set y-axis range | plt.ylim(0, 100) |
Conclusion
Use plt.xlabel() and plt.ylabel() to add descriptive labels to your Seaborn plots. Control the visible data range with plt.xlim() and plt.ylim() to focus on important data regions and improve plot readability.
