Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Axes | Section
Data Visualization & EDA
Seção 1. Capítulo 11
single

single

bookAxes

Deslize para mostrar o 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().

Tarefa

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.

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 11
single

single

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

some-alt