Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Axes | Section
Data Visualization & EDA
Avsnitt 1. Kapitel 11
single

single

bookAxes

Svep för att visa menyn

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

Uppgift

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.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 11
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt