Learning python, matplotlib, plotting, setting coordinate axis scales, text, pythonmatplotlib

Source: Internet
Author: User

Learning python, matplotlib, plotting, setting coordinate axis scales, text, pythonmatplotlib

Summarize how to set the scale and scale of the coordinate axis for matplotlib plotting.

Code:

From pylab import * from matplotlib. ticker import MultipleLocator, FormatStrFormatter xmajorLocator = MultipleLocator (20) # Set the x master scale label to a multiple of 20 xmajorFormatter = FormatStrFormatter ('% 1.1f ') # Set the format of the X axis label text xminorLocator = MultipleLocator (5) # Set the X axis secondary scale label to a multiple of 5 ymajorLocator = MultipleLocator (0.5) # Set the main scale label of the Y axis to a multiple of 0.5 ymajorFormatter = FormatStrFormatter ('% 1.1f') # set the format of the Y axis label text yminorLocator = MultipleLocator (0.1) # Set the secondary scale label of the Y axis to a multiple of 0.1 t = arange (0.0, 100.0, 1) s = sin (0.1 * pi * t) * exp (-t * 0.01) ax = subplot (111) # Note: It is generally set in ax. No more plot (t, s, '-- B *') is set in plot # Set the location of the master scale label, tag text format 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 secondary scale label without the label text ax. xaxis. set_minor_locator (xminorLocator) ax. yaxis. set_minor_locator (yminorLocator) ax. xaxis. grid (True, which = 'major') # Use the primary scale ax for the x axis grid. yaxis. grid (True, which = 'minor') # Use the secondary scale show () for the grid of the y axis ()

The plot is as follows:


If you carefully read the code, you can find that the methods of "MultipleLocator" and "FormatStrFormatter" are used to set the axis scale and text.

The two methods are from the ticker in the matplotlib installation library. py file; "MultipleLocator (Locator)" indicates to set the scale label to a multiple of Locator, and "FormatStrFormatter" indicates to set the label text format, in the Code, "% 1.1f" indicates that the last decimal point is retained, and the floating point is displayed.

The corresponding methods are as follows:


In addition to the above method, there is another method, that is, the xticks method (yticks, x, y indicates the corresponding coordinate axis), xticks usage can be viewed by entering the following code in python cmd:

import matplotlib.pyplot as plt help(plt.xticks) 

The Code is as follows:

import numpy as np import matplotlib.pyplot as plt fig,ax = plt.subplots() x = [1,2,3,4,5] y = [0,2,5,9,15] #ax is the axes instance group_labels = ['a', 'b','c','d','e'] plt.plot(x,y) plt.xticks(x, group_labels, rotation=0) plt.grid() plt.show() 

The plot is as follows:


The "plt. xticks" method is used to set the x-axis text. The label text uses the content in group_labels. Therefore, you can modify the content in group_labels as needed.

The Code is as follows:

Import matplotlib. pyplot as pl import numpy as np from matplotlib. ticker import MultipleLocator, FuncFormatter x = np. arange (0, 4 * np. pi, 0.01) y = np. sin (x) pl. figure (figsize = (10, 6) pl. plot (x, y, label = "$ sin (x) $") ax = pl. gca () def pi_formatter (x, pos): "converts a value to a scale text in the unit of pi/4" m = np. round (x/(np. pi/4) n = 4 if m % 2 = 0: m, n = m/2, n/2 if m % 2 = 0: m, n = m/2, n/2 if m = 0: return "0" if m = 1 and n = 1: return "$ \ pi $" if n = 1: return r "$ % d \ pi $" % m if m = 1: return r "$ \ frac {\ pi} {% d} $" % n return r "$ \ frac {% d \ pi} {% d} $" % (m, n) # Set the range of the two coordinate axes pl. ylim (-1.5, 1.5) pl. xlim (0, np. max (x) # Set the bottom margin pl. subplots_adjust (bottom = 0.15) pl. grid () # enable grid # The main scale is pi/4 ax. xaxis. set_major_locator (MultipleLocator (np. pi/4) # Use the pi_formatter function to calculate the main scale text ax. xaxis. set_major_formatter (FuncFormatter (pi_formatter) # The secondary scale is pi/20 ax. xaxis. set_minor_locator (MultipleLocator (np. pi/20) # Set the size of the scale text for tick in ax. xaxis. get_major_ticks (): tick. label1.set _ fontsize (16) pl. legend () pl. show ()

The plot is as follows:

The above is all the content of this small Editor. Thank you for your support for the help house.

Related Article

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.