Matplotlib Axis Configuration Functions
Matplotlib Reference Documentation
These functions are used to control the range, scale, ticks, grid lines, and appearance of the axes.
Function Overview
| Function | Description |
|---|---|
| xlim() / ylim() | Get or set the x/y axis range |
| xscale() / yscale() | Set the x/y axis scale: 'linear', 'log', 'symlog', 'logit' |
| xticks() / yticks() | Get or set x/y axis tick positions and labels |
| tick_params() | Adjust tick appearance parameters |
| ticklabel_format() | Set tick label format (scientific notation, etc.) |
| locator_params() | Control tick locator parameters |
| minorticks_on() / minorticks_off() | Show/hide minor ticks |
| rgrids() | Set radial grid lines for polar plots |
| thetagrids() | Set angular grid lines for polar plots |
| grid() | Turn grid lines on or off |
| axis() | Conveniently set axis range and appearance |
| box() | Toggle the Axes border lines |
| autoscale() | Autoscale the axes |
xlim() / ylim() - Axis Range
matplotlib.pyplot.xlim(*args, **kwargs) # 获取或设置 matplotlib.pyplot.ylim(*args, **kwargs)
xscale() / yscale() - Axis Scale
matplotlib.pyplot.xscale(value, **kwargs) matplotlib.pyplot.yscale(value, **kwargs) # value: 'linear', 'log', 'symlog', 'logit', 'function', 'asinh', ...
xticks() / yticks() - Ticks
matplotlib.pyplot.xticks(ticks=None, labels=None, **kwargs) matplotlib.pyplot.yticks(ticks=None, labels=None, **kwargs)
tick_params() - Tick Appearance
matplotlib.pyplot.tick_params(axis='both', **kwargs) # 常用参数: labelsize, labelcolor, rotation, direction, length, width, colors
ticklabel_format() - Tick Label Format
matplotlib.pyplot.ticklabel_format(*, axis='both', style='',
scilimits=None, useOffset=None, useLocale=None, useMathText=None)
grid() - Grid Lines
matplotlib.pyplot.grid(visible=None, which='major', axis='both',
**kwargs)
axis() - Convenience Function for Axis Appearance
matplotlib.pyplot.axis(*args, **kwargs) # 可接受字符串: 'on', 'off', 'equal', 'scaled', 'tight', 'auto', 'square' # 可接受列表: [xmin, xmax, ymin, ymax]
box() - Border Lines
matplotlib.pyplot.box(on=None)
autoscale() - Autoscale
matplotlib.pyplot.autoscale(enable=True, axis='both', tight=None)
locator_params() / minorticks
matplotlib.pyplot.locator_params(axis='both', tight=None, **kwargs) matplotlib.pyplot.minorticks_on() matplotlib.pyplot.minorticks_off()
Usage Examples
Example 1: Comprehensive Axis Configuration
Example
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.1, 100, 200)
y = x**2
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(14, 4),
layout='constrained')
# Left plot: linear coordinates + custom ticks and grid
ax1.plot(x, y, 'steelblue')
ax1.set_xlim(0, 100)
ax1.set_ylim(0, 10000)
ax1.set_xticks([0, 25, 50, 75, 100])
ax1.set_yticks([0, 2500, 5000, 7500, 10000])
ax1.grid(True, linestyle='--', alpha=0.4)
ax1.set_title('Linear: custom ticks')
# Middle plot: log coordinates
ax2.loglog(x, y, 'coral')
ax2.grid(True, which='both', linestyle=':', alpha=0.4)
ax2.set_title('loglog()')
# Right plot: scientific notation
ax3.plot(x, y, 'green')
ax3.ticklabel_format(style='sci', axis='y',
scilimits=(0, 0))
ax3.minorticks_on()
ax3.grid(True, which='major', alpha=0.5)
ax3.grid(True, which='minor', alpha=0.15)
ax3.set_title('Scientific notation + minor ticks')
plt.show()
import numpy as np
x = np.linspace(0.1, 100, 200)
y = x**2
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(14, 4),
layout='constrained')
# Left plot: linear coordinates + custom ticks and grid
ax1.plot(x, y, 'steelblue')
ax1.set_xlim(0, 100)
ax1.set_ylim(0, 10000)
ax1.set_xticks([0, 25, 50, 75, 100])
ax1.set_yticks([0, 2500, 5000, 7500, 10000])
ax1.grid(True, linestyle='--', alpha=0.4)
ax1.set_title('Linear: custom ticks')
# Middle plot: log coordinates
ax2.loglog(x, y, 'coral')
ax2.grid(True, which='both', linestyle=':', alpha=0.4)
ax2.set_title('loglog()')
# Right plot: scientific notation
ax3.plot(x, y, 'green')
ax3.ticklabel_format(style='sci', axis='y',
scilimits=(0, 0))
ax3.minorticks_on()
ax3.grid(True, which='major', alpha=0.5)
ax3.grid(True, which='minor', alpha=0.15)
ax3.set_title('Scientific notation + minor ticks')
plt.show()
Example 2: Detailed tick_params Configuration
Example
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
fig, ax = plt.subplots(figsize=(8, 4), layout='constrained')
ax.plot(x, y, 'steelblue', linewidth=2)
# Configure tick appearance in detail
ax.tick_params(axis='x', # x-axis only
rotation=45, # Rotate labels 45 degrees
labelsize=10, # Label size
labelcolor='blue', # Label color
direction='in', # Ticks point inward
length=6, # Tick length
width=1.5) # Tick width
ax.tick_params(axis='y',
labelsize=12,
labelcolor='red',
direction='inout', # Ticks on both the inside and outside
length=8,
width=2,
colors='red') # Both ticks and labels in red
ax.set_title('tick_params() Customization')
ax.grid(True, alpha=0.3)
plt.show()
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
fig, ax = plt.subplots(figsize=(8, 4), layout='constrained')
ax.plot(x, y, 'steelblue', linewidth=2)
# Configure tick appearance in detail
ax.tick_params(axis='x', # x-axis only
rotation=45, # Rotate labels 45 degrees
labelsize=10, # Label size
labelcolor='blue', # Label color
direction='in', # Ticks point inward
length=6, # Tick length
width=1.5) # Tick width
ax.tick_params(axis='y',
labelsize=12,
labelcolor='red',
direction='inout', # Ticks on both the inside and outside
length=8,
width=2,
colors='red') # Both ticks and labels in red
ax.set_title('tick_params() Customization')
ax.grid(True, alpha=0.3)
plt.show()
Example 3: axis() and box()
Example
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5, 5, 100)
fig, axes = plt.subplots(2, 2, figsize=(8, 8),
layout='constrained')
# 'equal': x and y unit lengths are equal
axes[0,0].plot(x, np.sin(x))
axes[0,0].axis('equal')
axes[0,0].set_title("axis('equal')")
# 'square': square Axes
axes[0,1].plot(x, np.cos(x))
axes[0,1].axis('square')
axes[0,1].set_title("axis('square')")
# 'tight': tightly fit to the data
axes[1,0].plot(x, x**2)
axes[1,0].axis('tight')
axes[1,0].set_title("axis('tight')")
# box(False) removes the border
axes[1,1].plot(x, np.sin(x))
axes[1,1].box(False)
axes[1,1].set_title('box(False) - No frame')
plt.show()
print("example: axis config demo")
import numpy as np
x = np.linspace(-5, 5, 100)
fig, axes = plt.subplots(2, 2, figsize=(8, 8),
layout='constrained')
# 'equal': x and y unit lengths are equal
axes[0,0].plot(x, np.sin(x))
axes[0,0].axis('equal')
axes[0,0].set_title("axis('equal')")
# 'square': square Axes
axes[0,1].plot(x, np.cos(x))
axes[0,1].axis('square')
axes[0,1].set_title("axis('square')")
# 'tight': tightly fit to the data
axes[1,0].plot(x, x**2)
axes[1,0].axis('tight')
axes[1,0].set_title("axis('tight')")
# box(False) removes the border
axes[1,1].plot(x, np.sin(x))
axes[1,1].box(False)
axes[1,1].set_title('box(False) - No frame')
plt.show()
print("example: axis config demo")
Other Extensions