Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Matplotlib Axis Configuration Functions

Matplotlib Axis Configuration Functions


Matplotlib Reference Documentation Matplotlib Reference Documentation

This group of functions is used to control the range, scale, ticks, grid lines, and appearance of the axes.

Function overview

function Function
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 the x/y axis tick positions and labels
tick_params() Adjust Tick Appearance Parameters
ticklabel_format() Set the tick label format (scientific notation, etc.)
locator_params() Control Tick Locator Parameters
minorticks_on() / minorticks_off() Show/hide minor ticks
rgrids() Set the radial grid lines of a polar plot
thetagrids() Set the angular grid lines of a polar plot
grid() Turn grid lines on or off
axis() Conveniently set the axis range and appearance
box() Toggle Axes border lines
autoscale() Auto-scale 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)
# 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() - convenience function for axis appearance

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-scaling

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: logarithmic 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 inward

               length

=

6

,

             

Tick length

               width

=

1.5

)

           

Tick width

ax.

tick_params

(

axis

=

'y'

,

               labelsize

=

12

,

               labelcolor

=

'red'

,

               direction

=

'inout'

,

   

Ticks both 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 have equal unit lengths

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': closely fit 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

(

"runoob: axis config demo"

)

Matplotlib Reference Documentation Matplotlib Reference Documentation