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

Swipe to show menu

book
Axes Customization

Ticks Customization

To adjust ticks on a plot, use the pyplot module's functions:

  • xticks for customizing the x-axis;

  • yticks for customizing the y-axis.

Both functions follow the same syntax and have two key parameters:

  • ticks defines the positions where the ticks should appear. You can use any array-like structure. To remove ticks entirely, pass an empty list;

  • labels assigns custom text to each tick position. This must be used together with the ticks parameter.

You can also include extra keyword arguments to style the labels and control their appearance.

Below is one of the graphs 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:

1234567891011121314151617181920
import pandas as pd import matplotlib.pyplot as plt # Loading the dataset with the average yearly temperatures in Boston and Seattle url = 'https://staging-content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/weather_data.csv' weather_df = pd.read_csv(url, index_col=0) # Plotting the data plt.plot(weather_df['Boston'], label='Boston') plt.plot(weather_df['Seattle'], label='Seattle') # Adding a title and legend 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) # Displaying the plot plt.show()
copy

Now every second year appears on the x-axis. This was achieved using the range() function with a step of 2 for the ticks argument.

Additionally, the tick labels were rotated 30 degrees counterclockwise using the rotation keyword for improved readability.

Tick labels can also be manually specified by passing a list to the labels argument (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).

123456789101112131415161718
import pandas as pd import matplotlib.pyplot as plt url = 'https://content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/weather_data.csv' weather_df = pd.read_csv(url, index_col=0) # Plotting the data plt.plot(weather_df['Boston'], label='Boston') plt.plot(weather_df['Seattle'], label='Seattle') # Adding title, legend, and labels plt.title('Boston and Seattle average yearly temperatures') plt.legend(loc='upper left') plt.xticks(range(1995, 2021, 2), rotation=30) plt.ylabel('Temperature, Β°F') # Displaying the plot 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'.

Note
Study More

You can explore more in the documentation: xlabel() and ylabel().

Task

Swipe to start coding

  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.

Solution

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

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

book
Axes Customization

Ticks Customization

To adjust ticks on a plot, use the pyplot module's functions:

  • xticks for customizing the x-axis;

  • yticks for customizing the y-axis.

Both functions follow the same syntax and have two key parameters:

  • ticks defines the positions where the ticks should appear. You can use any array-like structure. To remove ticks entirely, pass an empty list;

  • labels assigns custom text to each tick position. This must be used together with the ticks parameter.

You can also include extra keyword arguments to style the labels and control their appearance.

Below is one of the graphs 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:

1234567891011121314151617181920
import pandas as pd import matplotlib.pyplot as plt # Loading the dataset with the average yearly temperatures in Boston and Seattle url = 'https://staging-content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/weather_data.csv' weather_df = pd.read_csv(url, index_col=0) # Plotting the data plt.plot(weather_df['Boston'], label='Boston') plt.plot(weather_df['Seattle'], label='Seattle') # Adding a title and legend 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) # Displaying the plot plt.show()
copy

Now every second year appears on the x-axis. This was achieved using the range() function with a step of 2 for the ticks argument.

Additionally, the tick labels were rotated 30 degrees counterclockwise using the rotation keyword for improved readability.

Tick labels can also be manually specified by passing a list to the labels argument (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).

123456789101112131415161718
import pandas as pd import matplotlib.pyplot as plt url = 'https://content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/weather_data.csv' weather_df = pd.read_csv(url, index_col=0) # Plotting the data plt.plot(weather_df['Boston'], label='Boston') plt.plot(weather_df['Seattle'], label='Seattle') # Adding title, legend, and labels plt.title('Boston and Seattle average yearly temperatures') plt.legend(loc='upper left') plt.xticks(range(1995, 2021, 2), rotation=30) plt.ylabel('Temperature, Β°F') # Displaying the plot 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'.

Note
Study More

You can explore more in the documentation: xlabel() and ylabel().

Task

Swipe to start coding

  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.

Solution

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
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt