YouTip LogoYouTip

Matplotlib Ref Axis Config

Matplotlib Axis Configuration Functions | Online Tutorial * * * [![Image 1: Matplotlib Reference Documentation](https://example.com/images/up.gif) Matplotlib Reference Documentation](https://example.com/matplotlib/matplotlib-apiref.html) These functions are used to control the axis range, scale, ticks, gridlines and appearance. ## Function Overview | Function | Description | | --- | --- | | xlim() / ylim() | Get or set x/y axis range | | xscale() / yscale() | Set 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 polar plot radial grid lines | | thetagrids() | Set polar plot angle grid lines | | grid() | Turn grid lines on or off | | axis() | Convenient function to set axis range and appearance | | box() | Toggle Axes border lines | | autoscale() | Auto-scale axes | * * * ## xlim() / ylim() - Axis Range matplotlib.pyplot.xlim(*args, **kwargs) # Get or set 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)# Common parameters: labelsize, labelcolor, rotation, direction, length, width, colors ## ticklabel_format() - Tick 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() - Convenient Axis Appearance Function matplotlib.pyplot.axis(*args, **kwargs)# Acceptable strings: 'on', 'off', 'equal', 'scaled', 'tight', 'auto', 'square'# Acceptable list: [xmin, xmax, ymin, ymax] ## box() - Border Lines matplotlib.pyplot.box(on=None) ## autoscale() - Auto Scale 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 ## Instance 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 scale + 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 scale 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 ## Instance 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) # Detailed tick appearance configuration 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 inward length=6,# Tick length width=1.5)# Tick width ax.tick_params(axis='y', labelsize=12, labelcolor='red', direction='inout',# Ticks on both sides length=8, width=2, colors='red')# Both ticks and labels use red ax.set_title('tick_params() Customization') ax.grid(True, alpha=0.3) plt.show() ### Example 3: axis() and box() ## Instance 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': Fit tightly to data axes[1,0].plot(x, x**2) axes[1,0].axis('tight') axes[1,0].set_title("axis('tight')") # box(False) remove 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("tutorial: axis config demo") [![Image 2: Matplotlib Reference Documentation](https://example.com/images/up.gif) Matplotlib Reference Documentation](https://example.com/matplotlib/matplotlib-apiref.html)
← Matplotlib Ref BoxplotMatplotlib Ref 2D Plots β†’