Customize Left and Right Tick Marks in Matplotlib

As a Python developer, working extensively with Matplotlib, I’ve come to appreciate the power of fine-tuning plot aesthetics. One of the subtle yet impactful aspects of a plot is how the tick marks on the axes are displayed.

Especially when working with complex data visualizations, controlling the tick marks on the right and left sides of a plot can greatly enhance readability and professionalism.

In this article, I’ll share practical methods to customize Matplotlib tick marks using the tick_params function, focusing specifically on the right and left sides of the axes.

Matplotlib tick_params for the Right Side Ticks

When you create a plot in Python using Matplotlib, the default behavior is to show ticks on the bottom and left sides of the plot. However, sometimes you want to display ticks on the right side of the y-axis, for instance, when you’re comparing two datasets with different scales or simply want to improve the plot’s aesthetics.

Method 1: Enable Right Side Ticks Using tick_params

The simplest way to enable ticks on the right side is by using the tick_params method on the axis object. Here’s how I do it:

import matplotlib.pyplot as plt

# Sample data representing US state populations in millions
states = ['California', 'Texas', 'Florida', 'New York', 'Pennsylvania']
populations = [39.14, 29.53, 21.48, 19.45, 12.8]

fig, ax = plt.subplots()

ax.bar(states, populations, color='skyblue')
ax.set_title('US State Populations (millions)')

# Enable ticks on the right side of y-axis
ax.tick_params(axis='y', which='both', right=True, labelright=True)

plt.show()

You can refer to the screenshot below to see the output.

Right Tick Marks in Matplotlib

In this example, axis=’y’ targets the y-axis ticks. The argument right=True turns on the ticks on the right side, while labelright=True ensures the labels appear there as well. This method is easy and works well for most use cases.

Method 2: Customize Right Side Ticks Appearance

Sometimes, you want to customize the length, width, or color of the right-side ticks to differentiate them visually.

Here’s how I customize the right ticks’ appearance:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.plot([10, 20, 30, 40], [100, 200, 300, 400], marker='o')
ax.set_title('Sample Line Plot with Customized Right Ticks')

# Customize right ticks
ax.tick_params(axis='y', which='major', right=True, labelright=True,
               length=10, width=2, colors='red')

plt.show()

You can refer to the screenshot below to see the output.

Customize Right Tick Marks in Matplotlib

By setting length=10 and width=2, the right ticks become more prominent. The colors=’red’ argument adds a red color to the ticks and labels, making them visually distinct.

Control Left Side Ticks in Matplotlib

While left-side ticks are visible by default, there are scenarios when you might want to hide them or customize their appearance to better suit your visualization needs.

Method 1: Hide Left Side Ticks with tick_params

If you want to declutter your plot or emphasize the right side ticks, hiding the left side ticks is an effective approach.

Here’s how I hide the left side ticks:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.plot([5, 15, 25, 35], [50, 150, 250, 350], marker='s')
ax.set_title('Plot with Left Ticks Hidden')

# Hide left ticks and labels
ax.tick_params(axis='y', left=False, labelleft=False)

# Enable right ticks and labels for clarity
ax.tick_params(axis='y', right=True, labelright=True)

plt.show()

You can refer to the screenshot below to see the output.

Left Tick Marks in Matplotlib

This code snippet disables the left ticks and labels by setting left=False and labelleft=False. Simultaneously, it enables the right ticks and labels to maintain readability.

Method 2: Style Left Side Ticks with tick_params

If you want to keep left ticks but adjust their style to match your plot’s theme or highlight them differently, you can customize their appearance like this:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.plot([1, 2, 3, 4], [10, 20, 30, 40], marker='^')
ax.set_title('Styled Left Side Ticks')

# Customize left ticks appearance
ax.tick_params(axis='y', which='major', left=True, labelleft=True,
               length=8, width=1.5, colors='green')

plt.show()

You can refer to the screenshot below to see the output.

Customize Left Tick Marks in Matplotlib

Here, I set the left ticks to a green color with a longer length and thicker width, helping them stand out without overwhelming the plot.

Additional Tips for Using tick_params in Python Matplotlib

  • Targeting Major vs Minor Ticks: The which parameter accepts ‘major’, ‘minor’, or ‘both’. Use it to control the ticks you want to style.
  • Applying to X-axis: You can similarly control the top and bottom ticks on the x-axis using top=True or bottom=True.
  • Combining with Gridlines: For better readability, combine tick customization with gridlines. For example, enable gridlines only on major ticks.
  • Using with Twin Axes: When plotting with twin axes (ax.twinx()), controlling right and left ticks becomes crucial to avoid clutter.

Mastering how to control tick marks on the right and left sides of your Matplotlib plots significantly enhances your ability to create polished, professional data visualizations in Python. Whether you want to enable, hide, or style these ticks, the tick_params function offers a versatile and powerful way to customize your plots.

I encourage you to experiment with these methods in your projects, especially when visualizing US-centric data like state demographics or economic indicators. Proper tick management will make your charts easier to interpret and more visually appealing.

Related Articles You May Like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.