CodexBloom - Programming Q&A Platform

Matplotlib: How to implement custom tick frequency on a logarithmic scale for multiple subplots?

๐Ÿ‘€ Views: 0 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-14
matplotlib subplots logarithmic-scale Python

I've encountered a strange issue with I've been banging my head against this for hours. I'm reviewing some code and I keep running into After trying multiple solutions online, I still can't figure this out. I'm working on a project where I need to create multiple subplots with a logarithmic y-axis, but I'm struggling to set custom tick frequencies for each subplot. I'm using Matplotlib version 3.5.1. My goal is to have specific ticks that represent important data points, and I want these ticks to differ across subplots. Here's a simplified version of my code: ```python import matplotlib.pyplot as plt import numpy as np x = np.linspace(1, 100, 100) y1 = np.log10(x) y2 = np.log10(x * 10) y3 = np.log10(x ** 2) fig, axs = plt.subplots(3, 1, figsize=(8, 12)) axs[0].plot(x, y1) axs[1].plot(x, y2) axs[2].plot(x, y3) # Set the y-axis to logarithmic scale axs[0].set_yscale('log') axs[1].set_yscale('log') axs[2].set_yscale('log') plt.show() ``` When I run this code, the y-axis is set to logarithmic, but I don't know how to customize the ticks. I've tried using `axs[i].set_yticks()` with a list of values according to my needs, but it seems like the ticks arenโ€™t appearing correctly, especially for the second subplot. Instead, it just shows default ticks that are far apart and donโ€™t correlate with my data. Hereโ€™s what I tried to customize the ticks: ```python axs[0].set_yticks([1, 10, 100]) # for first subplot axs[1].set_yticks([10, 100, 1000]) # for second subplot axs[2].set_yticks([1, 10, 100]) # for third subplot ``` Even though I set the ticks, I still see the default logarithmic ticks alongside mine. I need to ensure that only my custom ticks are displayed. Additionally, is there a way to format the ticks to show them as integers (like `1`, `10`, `100` instead of `10^0`, `10^1`, etc.)? Any advice or examples would be greatly appreciated! I'm working on a API that needs to handle this. I'm coming from a different tech stack and learning Python. Could this be a known issue? I'm coming from a different tech stack and learning Python. This is my first time working with Python stable. Thanks in advance! How would you solve this?