Introduction
This lab will guide you through how to set the direction of tick labels in a Matplotlib plot. You will learn how to customize the direction of tick labels for both the x and y axes.
VM Tips
After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.
Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.
If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.
Import necessary modules
First, we need to import the necessary modules to create our plot. We will be using Matplotlib and AxisArtist from mpl_toolkits.
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist.axislines as axislines
Create a function to set up axes
We will create a function to set up our axes with the desired tick labels.
def setup_axes(fig, pos):
ax = fig.add_subplot(pos, axes_class=axislines.Axes)
ax.set_yticks([0.2, 0.8])
ax.set_xticks([0.2, 0.8])
return ax
Customize tick label direction
We will create three subplots to demonstrate different ways to customize the direction of tick labels.
Tick labels pointing outwards
In this step, we will create a subplot with tick labels pointing outwards.
fig = plt.figure(figsize=(6, 3))
fig.subplots_adjust(bottom=0.2)
ax = setup_axes(fig, 131)
for axis in ax.axis.values():
axis.major_ticks.set_tick_out(True)
Customized tick label direction
In this step, we will create a subplot with customized tick label direction.
ax = setup_axes(fig, 132)
ax.axis["left"].set_axis_direction("right")
ax.axis["bottom"].set_axis_direction("top")
ax.axis["right"].set_axis_direction("left")
ax.axis["top"].set_axis_direction("bottom")
Tick labels pointing outwards on one side
In this step, we will create a subplot with tick labels pointing outwards on one side.
ax = setup_axes(fig, 133)
ax.axis["left"].set_axis_direction("right")
ax.axis[:].major_ticks.set_tick_out(True)
ax.axis["left"].label.set_text("Long Label Left")
ax.axis["bottom"].label.set_text("Label Bottom")
ax.axis["right"].label.set_text("Long Label Right")
ax.axis["right"].label.set_visible(True)
ax.axis["left"].label.set_pad(0)
ax.axis["bottom"].label.set_pad(10)
plt.show()
Summary
In this lab, we learned how to customize the direction of tick labels in a Matplotlib plot using AxisArtist. By using the set_axis_direction() and major_ticks.set_tick_out() methods, we can create subplots with different tick label directions.