Matplotlib Python Basic usage

Source: Internet
Author: User

1. Simple drawing function

1 Import Matplotlib.pyplot as Plt 2 Import NumPy as NP  3 x = Np.linspace ( -1, 1,)4 y1 = x + 15plt.plot (x, y1)6 Plt.show ()

Np.linspace (-1, 1, 50)

Draw a function y=x+1 from 1 to 1 for 50 points

Import Matplotlib.pyplot as Plt Import  = Np.linspace ( -1, 1, 5)
= x1 * x1
y2 = x2 * X2plt.plot (x1, y1)
Plt.plot (x2, y2) plt.show ()

Change a function, change to y=x*x, can more intuitively feel the function of the third parameter of Np.linspace ()

Np.linspace () The third parameter is 5: Blue

Np.linspace () The third parameter is 50: red

2. Modify the style of the line

  Add a style directly to Plt.plot ()

1 ImportMatplotlib.pyplot as Plt2 ImportNumPy as NP3X1 = Np.linspace (-1, 1, 5)4x2 = Np.linspace (-1, 1, 50)5Y1 = x1 *X16y2 = x2 *X27 plt.plot (x1, y1)8Plt.plot (x2, y2, color="Gray", linewidth=10, linestyle="--")9Plt.show ()

  Color: Colors

LineWidth: line width

LineStyle: Line Style

After the modified image is

3. Modifying axes

  

1 ImportMatplotlib.pyplot as Plt2 ImportNumPy as NP3X1 = Np.linspace (-1, 1, 50)4Y1 = x1 *X15 plt.plot (x1, y1)6Plt.xlim (-1, 10)7Plt.ylim (-2, 3)8 9Plt.xlabel ("I am x")TenPlt.ylabel ("I am y") OnePlt.show ()

Plt.xlim (): Range of x-axis

Plt.ylim (): Range of y-axis

Plt.xlabel:x Axis Callout

Plt.ylabel:y Axis Callout

  Modify the value of an axis

1 ImportMatplotlib.pyplot as Plt2 ImportNumPy as NP3X1 = Np.linspace (-1, 1, 50)4Y1 = x1 *X15 plt.plot (x1, y1)6Plt.xlim (-1, 2)7Plt.ylim (-2, 3)8 9Plt.xlabel ("I am x")TenPlt.ylabel ("I am y") One  ANew_ticks = Np.linspace (-1, 2, 5) - plt.xticks (new_ticks) -Plt.yticks ([-2, 0, 2, 3], ["very Bad","just so","Good","Perfect"]) thePlt.show ()

  Change the font and add special symbols (note escaping) don't mention it.

  

Matplotlib Python Basic usage

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.