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

bookAxes Customization

Ticks Customization

To customize ticks, use:

  • xticks for the x-axis,
  • yticks for the y-axis.

Both accept:

  • ticks β€” positions of ticks (empty list removes ticks);
  • labels β€” custom text for those positions.

Additional keyword arguments let you style tick labels (e.g., rotation, font size).

Below is a plot using these tools:

Plot with default ticks

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:

123456789101112131415
import pandas as pd import matplotlib.pyplot as plt 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) 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) plt.show()
copy

Every second year is displayed on the x-axis thanks to range(1995, 2021, 2). Labels are rotated 30Β° for readability. Tick labels can also be manually set by passing a list to labels.

Axes Labels Customization

Use xlabel() and ylabel() to set axis labels. Each takes a single string.

123456789101112131415
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) 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) plt.ylabel('Temperature, Β°F') plt.show()
copy

You can style labels with options such as fontsize and color. The loc argument controls label placement:

  • For x-labels: 'left', 'center', 'right';
  • For y-labels: 'top', 'center', '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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 3
single

single

Ask AI

expand

Ask AI

ChatGPT

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

Suggested prompts:

How do I add both x and y axis labels to my plot?

Can you show how to change the font size and color of axis labels?

What does the `loc` argument do for axis labels?

close

bookAxes Customization

Swipe to show menu

Ticks Customization

To customize ticks, use:

  • xticks for the x-axis,
  • yticks for the y-axis.

Both accept:

  • ticks β€” positions of ticks (empty list removes ticks);
  • labels β€” custom text for those positions.

Additional keyword arguments let you style tick labels (e.g., rotation, font size).

Below is a plot using these tools:

Plot with default ticks

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:

123456789101112131415
import pandas as pd import matplotlib.pyplot as plt 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) 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) plt.show()
copy

Every second year is displayed on the x-axis thanks to range(1995, 2021, 2). Labels are rotated 30Β° for readability. Tick labels can also be manually set by passing a list to labels.

Axes Labels Customization

Use xlabel() and ylabel() to set axis labels. Each takes a single string.

123456789101112131415
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) 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) plt.ylabel('Temperature, Β°F') plt.show()
copy

You can style labels with options such as fontsize and color. The loc argument controls label placement:

  • For x-labels: 'left', 'center', 'right';
  • For y-labels: 'top', 'center', '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
single

single

some-alt