Let's Cope with Axes
Setting label names:
To set label names, we need to set a plot variable and use:
python912# g is the plot variable.g.set(xlabel = 'x_name', ylabel = 'y_name')
X(y)ticks rotation:
To rotate labels, we need to use:
python9123# n is the angleplt.xticks(rotation = n)plt.yticks(rotation = n)
Task
Swipe to start coding
- Import
seaborn
withsns
alias. - Import
matplotlib.pyplot
withplt
alias. - Import
pandas
withpd
alias. - Set label names
'Population'
(Oy axis) &'Years(2012-2022)'
(Ox axis). - Rotate labels along the Ox axis by 45 degrees.
- Show the plot.
Solution
99
1
2
3
4
5
6
7
8
9
10
11
12
13
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/c5b4ea8f-8a30-439f-9625-ddf2effbd9ac/lineplothue.csv')
g = sns.lineplot(x = 'year', y = 'population', hue = 'season', data = df)
g.set(ylabel = 'Population', xlabel = 'Years(2012-2022)')
plt.xticks(rotation = 45)
plt.show()
Everything was clear?
Thanks for your feedback!
Section 3. Chapter 3
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Import libraries needed
___
___
___
# Reading the file
df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/c5b4ea8f-8a30-439f-9625-ddf2effbd9ac/lineplothue.csv')
# Setting the variable
g = sns.lineplot(x = 'year', y = 'population', hue = 'season', data = df)
# Set label names
___.___(ylabel = '___', ___ = '___')
# Rotate
___.___(rotation = ___)
# Show the plot
___
Ask AI
Ask anything or try one of the suggested questions to begin our chat