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 theticks
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:
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()
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
).
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()
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'
.
Swipe to start coding
- Use the correct function to set
data_linear
as x-axis ticks. - Use the correct function to set
'x'
as the x-axis label. - Use
'right'
as the location for the x-axis label. - Use the correct function to set
'y'
as the y-axis label. - Use
'top'
as the location for the y-axis label. - Set
rotation
parameter to0
for the y-axis label.
Solution
Thanks for your feedback!