Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Axes Customization | Plots Customization
Ultimate Visualization with Python
course content

Course Content

Ultimate Visualization with Python

Ultimate Visualization with Python

1. Matplotlib Introduction
2. Creating Commonly Used Plots
3. Plots Customization
4. More Statistical Plots
5. Plotting with Seaborn

bookAxes 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:

123456789101112
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()
copy

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).

123456789101112
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()
copy

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'.

You can explore more in the documentation: xlabel, ylabel.

Task

  1. Use the correct function to set data_linear as x-axis ticks.
  2. Use the correct function to set 'x' as the x-axis label.
  3. Use 'right' as the location for the x-axis label.
  4. Use the correct function to set 'y' as the y-axis label.
  5. Use 'top' as the location for the y-axis label.
  6. Set rotation parameter to 0 for the y-axis label.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 3
toggle bottom row

bookAxes 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:

123456789101112
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()
copy

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).

123456789101112
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()
copy

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'.

You can explore more in the documentation: xlabel, ylabel.

Task

  1. Use the correct function to set data_linear as x-axis ticks.
  2. Use the correct function to set 'x' as the x-axis label.
  3. Use 'right' as the location for the x-axis label.
  4. Use the correct function to set 'y' as the y-axis label.
  5. Use 'top' as the location for the y-axis label.
  6. Set rotation parameter to 0 for the y-axis label.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 3
toggle bottom row

bookAxes 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:

123456789101112
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()
copy

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).

123456789101112
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()
copy

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'.

You can explore more in the documentation: xlabel, ylabel.

Task

  1. Use the correct function to set data_linear as x-axis ticks.
  2. Use the correct function to set 'x' as the x-axis label.
  3. Use 'right' as the location for the x-axis label.
  4. Use the correct function to set 'y' as the y-axis label.
  5. Use 'top' as the location for the y-axis label.
  6. Set rotation parameter to 0 for the y-axis label.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your 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:

123456789101112
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()
copy

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).

123456789101112
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()
copy

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'.

You can explore more in the documentation: xlabel, ylabel.

Task

  1. Use the correct function to set data_linear as x-axis ticks.
  2. Use the correct function to set 'x' as the x-axis label.
  3. Use 'right' as the location for the x-axis label.
  4. Use the correct function to set 'y' as the y-axis label.
  5. Use 'top' as the location for the y-axis label.
  6. Set rotation parameter to 0 for the y-axis label.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 3. Chapter 3
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt