Conteúdo do Curso
Ultimate Visualization with Python
Ultimate Visualization with Python
Axes Customization
Ticks Customization
For adjusting ticks, the pyplot
module has two functions with identical syntax:
xticks()
for the x-axis;yticks()
for the y-axis.
Both functions have two most important parameters:
ticks
specifies where you want the ticks to appear (use an array-like type). You can remove ticks by passing an empty list;labels
assigns labels to the ticks at their specified locations. This parameter must be used alongside the ticks parameter.
You can also provide additional keyword arguments to control the appearance of the labels.
Now it’s time for some examples. Here is one of the graphs we have recently created:
Everything seems to be pretty fine with this plot, however, it would be better to have more years on the x-axis in this range (1995-2020). Let’s use xticks()
for this purpose:
import pandas as pd import matplotlib.pyplot as plt url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/weather_data.csv' # Loading the dataset with the average yearly temperatures in Boston and Seattle weather_df = pd.read_csv(url, index_col=0) plt.plot(weather_df['Boston'], label='Boston') plt.plot(weather_df['Seattle'], label='Seattle') plt.title('Boston and Seattle average yearly temperatures') plt.legend(loc='upper left') # Setting the x-ticks coordinates and their rotation plt.xticks(range(1995, 2021, 2), rotation=30) plt.show()
Now there is every second year in this range on the x-axis. We accomplished this using the range()
function (with the step parameter 2
) for the ticks
argument.
Moreover, we used a keyword argument rotation
to rotate the ticks labels 30 degrees counterclockwise for better readability.
We could also specify the list of labels for the ticks via setting the labels
(e.g, labels = ['label1', 'label2']
).
Axes Labels Customization
You can use xlabel()
and ylabel()
functions from the pyplot
module to set the labels for the x-axis and y-axis. These functions require only one parameter: the label itself (a string
).
import pandas as pd import matplotlib.pyplot as plt url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/weather_data.csv' weather_df = pd.read_csv(url, index_col=0) plt.plot(weather_df['Boston'], label='Boston') plt.plot(weather_df['Seattle'], label='Seattle') plt.title('Boston and Seattle average yearly temperatures') plt.legend(loc='upper left') plt.xticks(range(1995, 2021, 2), rotation=30) # Setting the label for the y-axis plt.ylabel('Temperature, °F') plt.show()
It is also possible to modify the label apperance, for instance, set the font size via fontsize
keyword argument or its color via color
keyword argument.
In addition, there is loc
parameter which specifies the label location (center
by default).
- For x-axis label
'left'
,'center'
and'right'
are possible values; - For y-axis instead of
'left'
and'right'
there is'top'
and'bottom'
.
Tarefa
- Use the correct function to set
data_linear
as x-axis ticks. - Use the correct function to set
'x'
as the x-axis label. - Use
'right'
as the location for the x-axis label. - Use the correct function to set
'y'
as the y-axis label. - Use
'top'
as the location for the y-axis label. - Set
rotation
parameter to0
for the y-axis label.
Obrigado pelo seu feedback!
Axes Customization
Ticks Customization
For adjusting ticks, the pyplot
module has two functions with identical syntax:
xticks()
for the x-axis;yticks()
for the y-axis.
Both functions have two most important parameters:
ticks
specifies where you want the ticks to appear (use an array-like type). You can remove ticks by passing an empty list;labels
assigns labels to the ticks at their specified locations. This parameter must be used alongside the ticks parameter.
You can also provide additional keyword arguments to control the appearance of the labels.
Now it’s time for some examples. Here is one of the graphs we have recently created:
Everything seems to be pretty fine with this plot, however, it would be better to have more years on the x-axis in this range (1995-2020). Let’s use xticks()
for this purpose:
import pandas as pd import matplotlib.pyplot as plt url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/weather_data.csv' # Loading the dataset with the average yearly temperatures in Boston and Seattle weather_df = pd.read_csv(url, index_col=0) plt.plot(weather_df['Boston'], label='Boston') plt.plot(weather_df['Seattle'], label='Seattle') plt.title('Boston and Seattle average yearly temperatures') plt.legend(loc='upper left') # Setting the x-ticks coordinates and their rotation plt.xticks(range(1995, 2021, 2), rotation=30) plt.show()
Now there is every second year in this range on the x-axis. We accomplished this using the range()
function (with the step parameter 2
) for the ticks
argument.
Moreover, we used a keyword argument rotation
to rotate the ticks labels 30 degrees counterclockwise for better readability.
We could also specify the list of labels for the ticks via setting the labels
(e.g, labels = ['label1', 'label2']
).
Axes Labels Customization
You can use xlabel()
and ylabel()
functions from the pyplot
module to set the labels for the x-axis and y-axis. These functions require only one parameter: the label itself (a string
).
import pandas as pd import matplotlib.pyplot as plt url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/weather_data.csv' weather_df = pd.read_csv(url, index_col=0) plt.plot(weather_df['Boston'], label='Boston') plt.plot(weather_df['Seattle'], label='Seattle') plt.title('Boston and Seattle average yearly temperatures') plt.legend(loc='upper left') plt.xticks(range(1995, 2021, 2), rotation=30) # Setting the label for the y-axis plt.ylabel('Temperature, °F') plt.show()
It is also possible to modify the label apperance, for instance, set the font size via fontsize
keyword argument or its color via color
keyword argument.
In addition, there is loc
parameter which specifies the label location (center
by default).
- For x-axis label
'left'
,'center'
and'right'
are possible values; - For y-axis instead of
'left'
and'right'
there is'top'
and'bottom'
.
Tarefa
- Use the correct function to set
data_linear
as x-axis ticks. - Use the correct function to set
'x'
as the x-axis label. - Use
'right'
as the location for the x-axis label. - Use the correct function to set
'y'
as the y-axis label. - Use
'top'
as the location for the y-axis label. - Set
rotation
parameter to0
for the y-axis label.
Obrigado pelo seu feedback!
Axes Customization
Ticks Customization
For adjusting ticks, the pyplot
module has two functions with identical syntax:
xticks()
for the x-axis;yticks()
for the y-axis.
Both functions have two most important parameters:
ticks
specifies where you want the ticks to appear (use an array-like type). You can remove ticks by passing an empty list;labels
assigns labels to the ticks at their specified locations. This parameter must be used alongside the ticks parameter.
You can also provide additional keyword arguments to control the appearance of the labels.
Now it’s time for some examples. Here is one of the graphs we have recently created:
Everything seems to be pretty fine with this plot, however, it would be better to have more years on the x-axis in this range (1995-2020). Let’s use xticks()
for this purpose:
import pandas as pd import matplotlib.pyplot as plt url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/weather_data.csv' # Loading the dataset with the average yearly temperatures in Boston and Seattle weather_df = pd.read_csv(url, index_col=0) plt.plot(weather_df['Boston'], label='Boston') plt.plot(weather_df['Seattle'], label='Seattle') plt.title('Boston and Seattle average yearly temperatures') plt.legend(loc='upper left') # Setting the x-ticks coordinates and their rotation plt.xticks(range(1995, 2021, 2), rotation=30) plt.show()
Now there is every second year in this range on the x-axis. We accomplished this using the range()
function (with the step parameter 2
) for the ticks
argument.
Moreover, we used a keyword argument rotation
to rotate the ticks labels 30 degrees counterclockwise for better readability.
We could also specify the list of labels for the ticks via setting the labels
(e.g, labels = ['label1', 'label2']
).
Axes Labels Customization
You can use xlabel()
and ylabel()
functions from the pyplot
module to set the labels for the x-axis and y-axis. These functions require only one parameter: the label itself (a string
).
import pandas as pd import matplotlib.pyplot as plt url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/weather_data.csv' weather_df = pd.read_csv(url, index_col=0) plt.plot(weather_df['Boston'], label='Boston') plt.plot(weather_df['Seattle'], label='Seattle') plt.title('Boston and Seattle average yearly temperatures') plt.legend(loc='upper left') plt.xticks(range(1995, 2021, 2), rotation=30) # Setting the label for the y-axis plt.ylabel('Temperature, °F') plt.show()
It is also possible to modify the label apperance, for instance, set the font size via fontsize
keyword argument or its color via color
keyword argument.
In addition, there is loc
parameter which specifies the label location (center
by default).
- For x-axis label
'left'
,'center'
and'right'
are possible values; - For y-axis instead of
'left'
and'right'
there is'top'
and'bottom'
.
Tarefa
- Use the correct function to set
data_linear
as x-axis ticks. - Use the correct function to set
'x'
as the x-axis label. - Use
'right'
as the location for the x-axis label. - Use the correct function to set
'y'
as the y-axis label. - Use
'top'
as the location for the y-axis label. - Set
rotation
parameter to0
for the y-axis label.
Obrigado pelo seu feedback!
Ticks Customization
For adjusting ticks, the pyplot
module has two functions with identical syntax:
xticks()
for the x-axis;yticks()
for the y-axis.
Both functions have two most important parameters:
ticks
specifies where you want the ticks to appear (use an array-like type). You can remove ticks by passing an empty list;labels
assigns labels to the ticks at their specified locations. This parameter must be used alongside the ticks parameter.
You can also provide additional keyword arguments to control the appearance of the labels.
Now it’s time for some examples. Here is one of the graphs we have recently created:
Everything seems to be pretty fine with this plot, however, it would be better to have more years on the x-axis in this range (1995-2020). Let’s use xticks()
for this purpose:
import pandas as pd import matplotlib.pyplot as plt url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/weather_data.csv' # Loading the dataset with the average yearly temperatures in Boston and Seattle weather_df = pd.read_csv(url, index_col=0) plt.plot(weather_df['Boston'], label='Boston') plt.plot(weather_df['Seattle'], label='Seattle') plt.title('Boston and Seattle average yearly temperatures') plt.legend(loc='upper left') # Setting the x-ticks coordinates and their rotation plt.xticks(range(1995, 2021, 2), rotation=30) plt.show()
Now there is every second year in this range on the x-axis. We accomplished this using the range()
function (with the step parameter 2
) for the ticks
argument.
Moreover, we used a keyword argument rotation
to rotate the ticks labels 30 degrees counterclockwise for better readability.
We could also specify the list of labels for the ticks via setting the labels
(e.g, labels = ['label1', 'label2']
).
Axes Labels Customization
You can use xlabel()
and ylabel()
functions from the pyplot
module to set the labels for the x-axis and y-axis. These functions require only one parameter: the label itself (a string
).
import pandas as pd import matplotlib.pyplot as plt url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/weather_data.csv' weather_df = pd.read_csv(url, index_col=0) plt.plot(weather_df['Boston'], label='Boston') plt.plot(weather_df['Seattle'], label='Seattle') plt.title('Boston and Seattle average yearly temperatures') plt.legend(loc='upper left') plt.xticks(range(1995, 2021, 2), rotation=30) # Setting the label for the y-axis plt.ylabel('Temperature, °F') plt.show()
It is also possible to modify the label apperance, for instance, set the font size via fontsize
keyword argument or its color via color
keyword argument.
In addition, there is loc
parameter which specifies the label location (center
by default).
- For x-axis label
'left'
,'center'
and'right'
are possible values; - For y-axis instead of
'left'
and'right'
there is'top'
and'bottom'
.
Tarefa
- Use the correct function to set
data_linear
as x-axis ticks. - Use the correct function to set
'x'
as the x-axis label. - Use
'right'
as the location for the x-axis label. - Use the correct function to set
'y'
as the y-axis label. - Use
'top'
as the location for the y-axis label. - Set
rotation
parameter to0
for the y-axis label.