You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matplotlib双对数图仅Y轴有次要刻度?X轴次要刻度如何配置?

Fixing Minor Ticks on All Axes in Matplotlib Log-Log Plots

I’ve run into this exact issue before with Matplotlib’s log-log plots—minor ticks not showing up on the X-axis can be frustrating, but it’s easy to fix with a few explicit settings. Here’s how to get all four axes (bottom, top, left, right) displaying both major and minor ticks:

Corrected Code

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import LogFormatter, LogLocator

# Create sample log-spaced data
x = np.logspace(1, 5, 100)  # X values from 10^1 to 10^5
y = x ** 1.5  # Example Y vs X relationship

# Initialize the plot and axes
fig, ax = plt.subplots(figsize=(8, 6))

# Plot data with log-log scale
ax.loglog(x, y, label='Sample Data')

# Enable ticks (major AND minor) on all four axis sides
ax.tick_params(axis='x', which='both', bottom=True, top=True, 
               labelbottom=True, labeltop=False)  # Keep labels only on bottom X
ax.tick_params(axis='y', which='both', left=True, right=True, 
               labelleft=True, labelright=False)  # Keep labels only on left Y

# Set log-specific locators to ensure minor ticks are placed correctly
ax.xaxis.set_major_locator(LogLocator())
ax.xaxis.set_minor_locator(LogLocator(subs='auto'))  # Auto-place minor ticks between majors
ax.yaxis.set_major_locator(LogLocator())
ax.yaxis.set_minor_locator(LogLocator(subs='auto'))

# Use LogFormatter for proper log-scale tick labels (avoids issues with ScalarFormatter)
ax.xaxis.set_major_formatter(LogFormatter(labelOnlyBase=False))
ax.xaxis.set_minor_formatter(LogFormatter(labelOnlyBase=False))
ax.yaxis.set_major_formatter(LogFormatter(labelOnlyBase=False))
ax.yaxis.set_minor_formatter(LogFormatter(labelOnlyBase=False))

# Add plot labels and legend
ax.set_xlabel('X Axis (Log Scale)')
ax.set_ylabel('Y Axis (Log Scale)')
ax.legend()

# Display the plot
plt.show()

Key Fixes Explained

  • Enable all ticks: The tick_params() calls explicitly turn on both major and minor ticks for the top/bottom X-axis and left/right Y-axis. You can adjust labeltop/labelright if you want labels on those sides too.
  • Log Locators: LogLocator is designed specifically for log scales. The subs='auto' parameter tells Matplotlib to automatically place minor ticks between major log ticks (e.g., 2, 3, ..., 9 between 10^1 and 10^2).
  • Log Formatters: Replacing ScalarFormatter with LogFormatter ensures that tick labels (especially minor ones) are displayed correctly for log scales. The labelOnlyBase=False setting shows labels for minor ticks instead of just the major base values.

If your original data has a smaller range (e.g., only one order of magnitude), LogLocator(subs='auto') will still handle minor ticks appropriately.

内容的提问来源于stack exchange,提问作者npross

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 02:28:29