Python data graphical--matplotlib basic application

Source: Internet
Author: User
Tags dashed line rand install matplotlib

Matplotlib is a common data visualization tool used in Python, and is a bit similar to MATLAB. Calls are simple and powerful. You can install it from the command line pip install matplotlib under Windows.

Here are some examples of basic use:

1. Draw a straight line

First generate a set of data on line y = 5 * x + 5 by NumPy and then draw it on the chart

1 Import NumPy as NP 2 Import Matplotlib.pyplot as Plot 3 4 x = Np.linspace (1, ten, ten)5 y = 5 * x + 567#  Plot.figur E ()8plot.plot (x, y)9 plot.show ()

Run the above code with the following results:

2. Draw a line chart

The Matplotlib method that draws a line chart call is just the same as the data generated using NumPy.

1 Import NumPy as NP 2 Import Matplotlib.pyplot as Plot 3 4 x = Np.linspace (1, ten, ten)5 y = np.random.normal (1, 5, ten)67
   plot.figure ()8plot.plot (x, y)9 plot.show ()

Because of the random number involved, the results of each run will be different. A running result is as follows:

 

3. Draw a curve with different styles at the same time

The plot method can draw multiple lines at the same time, and supports different curves with different styles and colors to display. In the following code, the plot method has 3 parameters for a group of 3 groups, and the parameters for each group are x-axis, y-coordinate, and style.

Style usage:

Format:

' [Color][marker][line] '

The first letter of the style represents the color, and the supported colors are: R (Red), g (green), B (blue), C (cyan), M (Megenta), Y (Yellow), W (White), K (black).

The second part of the style represents the fill symbol for the line, which can be written:-(dashed), + (plus), ^ (positive triangle up), S (square), O (Round), and so on. You can also use two padding methods, such as ' ro--' for filling at the same time with a red dashed line and a solid circle.

Import NumPy as NP Import  = Np.linspace ( -10, ten,'r--'g^ ' ' c+ ' ) plot.show ()

4. Plot scatter plots

Call the Scatter method to draw a scatter plot.

Import NumPy as NP Import  = np.linspace (1, ten, ten= Np.linspace (1, ten, ten) plot.scatter (x, y) Plot.ylabel (' y value ' ) Plot.xlabel ('xscale') plot.title ('Scatter Figure') plot.show ()

Output:

Different scatter plots can be obtained by setting different parameters:

1 ImportNumPy as NP2 ImportMatplotlib.pyplot as Plot3 4 #fixed random number of seed so that it produces the same results each time it runs5Np.random.seed (1)6 7num = 1008Edgeradius = 0.59x =np.random.rand (num)Teny =np.random.rand (num) OneArea = (200 *Np.random.rand (num)) Acolor =np.sqrt (area) - #create an area with different styles to show points of different partitions -Trianglearea = Np.ma.masked_where (x <=Edgeradius, area) theDiamondarea = Np.ma.masked_where (x >Edgeradius, area) -Plot.scatter (x, Y, S=trianglearea, marker='o', c=color) -Plot.scatter (x, Y, S=diamondarea, marker='D', c=color) -Plot.title ('Random Scatter') +Plot.show ()

5. Draw histogram and multiple graphs

In the following example, the figure function specifies the number and size scale of the chart, Suptitle sets the caption, subplot specifies the position of the current drawing (parameter 221 represents a 2x2 total of four graphics areas, and the last 1 represents the first graphics area). The bar, scatter, plot, and pie functions are plotted with bars, scatter plots, fold (straight) lines, and pie charts, respectively.

ImportMatplotlib.pyplot as Plotlabels= ['A','B','C']values= [1, 2, 3]plot.figure (1, figsize= (9, 6)) Plot.suptitle ('Multiple Plots') Plot.subplot (221) Plot.bar (labels, values) Plot.subplot (222) Plot.scatter (labels, values) Plot.subplot (223) Plot.plot (labels, values) Plot.subplot (224) Plot.pie (values, labels=labels) plot.show ()

This address: www.cnblogs.com/laishenghao/p/9573465.html

Author Blog: (̀ω ́) y

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.