Matplotlib Scale Setting detailed

Source: Internet
Author: User

today, We summarize how to set the scale of matplotlib drawing, hoping to help the students who are learning matplotlib .

Tick Locating and formatting

The module includes many classes to support the full scale position and format of the Configuration. Although Locators is not related to the main scale or small scale, they are used by the Axis class to support major and small scale positions and formatting. In general, the scale position and format are provided and are often the most commonly used FORM.

Default format

When the x - axis data is plotted in a small centralized area of a large interval, the default format will take Effect. To reduce the likelihood of overlapping of scale labels, the scale is marked in an empty area between the fixed intervals. For Example:

Ax.plot (np.arange), Range (10))

The form of expression is as Follows:

The scale is only marked with 0-9 and an interval of +2e3 . If you do not want this form, you can turn off the use of the interval callout in the default Formatting.

Ax.get_xaxis (). get_major_formatter (). set_useoffset (False)

Set rcparam axes.formatter.useoffset=false to turn off globally, or to set a different format.

Scale position

The Locator class is the base class for all scale Locators. the Locators is responsible for automatically adjusting the visual interval based on the range of data and the selection of the tick Position. multiplelocator is a useful semi-automatic scale Locator. You can initialize settings through the base class, and so On.

The Locator sub-class is defined as Follows:

Nulllocator

No Ticks

Fixedlocator

Tick Locations is fixed

indexlocator

locator for index plots (e.g., where x = Range (len (y)))

linearlocator

evenly spaced ticks from min to Max

loglocator

logarithmically ticks from min to Max

symmetricalloglocator

locator norm, works like the Loglocator for the part outside of the threshold and add 0 if inside the limits

multiplelocator

ticks and range are a multiple of base; Either integer or float

oldautolocator

choose a MultipleLocator and Dyamically reassign it for intelligent ticking during navigation

maxnlocator

finds up to a max number of ticks at Nice Locations

autolocator

maxnlocator with simple defaults. This is the default tick locator for most plotting.

Autominorlocator

Locator for minor ticks when the axis was linear and the major ticks are uniformly spaced. It subdivides The major tick interval into a specified number of minor intervals, defaulting to 4 or 5 depending on the MA Jor Interval.

You can inherit Locator to define your own Locator. You must override the ___call__ method, which returns a sequence of positions, and you may also want to override the Autoscale method to set the visual interval based on the extent of the Data.

If you want to rewrite the default locator, use either the above or the commonly used locator to Pass it to x or y axis object. The relevant methods are as Follows:

Ax.xaxis.set_major_locator (xmajorlocator) ax.xaxis.set_minor_locator (xminorlocator) ax.yaxis.set_major_locator ( Ymajorlocator) Ax.yaxis.set_minor_locator (yminorlocator)

Scale format

The scale format is controlled by classes inherited by Formatter. Formatter A string that only acts on a single tick value and returns an Axis.
Refer to the official documentation for the related Subclasses.

You can also override the __all__ method to inherit the Formatter base class to set your own Formatter.

To control the formatting of a major or small scale callout, use either of the following methods:

Ax.xaxis.set_major_formatter (xmajorformatter) ax.xaxis.set_minor_formatter (xminorformatter) ax.yaxis.set_major_ Formatter (ymajorformatter) Ax.yaxis.set_minor_formatter (yminorformatter)

Set a scale callout

Examples of prototypes:

Set_xticklabels (labels, fontdict=none, minor=false, **kwargs)

A comprehensive example (1) is as Follows:

Sets the label for the specified position to be changed to another callout:

...

Plt.xticks ([-np.pi,-np.pi/2, 0, np.pi/2, np.pi],

[r ' $-\pi$ ', R ' $-\pi/2$ ', R ' $0$ ', R ' $+\pi/2$ ', R ' $+\pi$ '])

Plt.yticks ([-1, 0, +1],

[r ' $-1$ ', R ' $0$ ', R ' $+1$ '])

...

A comprehensive example (2) is as Follows:

Sets the Axis major and minor Scales.

#!/usr/bin/env python#-*-coding:utf-8-*-#---------------------------------------------------# Demo Sets the axis main tick label and the Sub-scale label in matplotlib .

# for the minor scale display , If you want to use the default settings as long as matplotlib.pyplot.minorticks_on ()

#---------------------------------------------------

Import NumPy as Npimport Matplotlib.pyplot as Pltfrom matplotlib.ticker import multiplelocator, formatstrformatter

#---------------------------------------------------

Xmajorlocator = multiplelocator # Sets the x main tick label to multiples of

Xmajorformatter = Formatstrformatter ('%5.1f ') # sets The format of the x-axis label text

Xminorlocator = multiplelocator (5) # Sets the x - axis Secondary scale label to a multiple of 5

Ymajorlocator = multiplelocator (0.5) # sets the y - Axis main tick label to a multiple of 0.5

Ymajorformatter = Formatstrformatter ('%1.1f ') # sets The format of the y-axis label text

Yminorlocator = Multiplelocator (0.1) # sets this y - Axis minor tick label to a multiple of 0.1

t = np.arange (0.0, 100.0, 1)

s = Np.sin (0.1*np.pi*t) *np.exp (-t*0.01)

Ax = plt.subplot (111) # Note : typically set in ax , no longer set in plot

Plt.plot (t,s, '--r* ')

# Set the position of the main tick label , the format of the label text

Ax.xaxis.set_major_locator (xmajorlocator)

Ax.xaxis.set_major_formatter (xmajorformatter)

Ax.yaxis.set_major_locator (ymajorlocator)

Ax.yaxis.set_major_formatter (ymajorformatter)

# Displays the position of the Sub-scale label with no label text

Ax.xaxis.set_minor_locator (xminorlocator)

Ax.yaxis.set_minor_locator (yminorlocator)

Ax.xaxis.grid (True, which= ' Major ') uses the main scale #x axis grid

Ax.yaxis.grid (True, which= ' minor ') uses a sub-scale #y axis grid

Plt.show ()

##########################################################

The image form is as Follows:


Article from: Blog Park /chris*chen

Matplotlib Scale Setting detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.