Python Data Analysis Toolkit (4)--matplotlib (ii)

Source: Internet
Author: User
Tags explode

The previous article describes how to draw some simple diagrams with matplotlib. You usually need to add a lot of callout information on the diagram, such as adding a legend.

1 ImportMatplotlib.pyplot as Plt2 ImportNumPy as NP3 4 5T=np.arange (0.,5.,0.2)6 #Here we set the line color and linear, and can actually set the line width and other parameters7Plt.plot (T,T*10,'r--', T**2,'BS', T**3,'g^')8 #The legend is used to set the legend, and when the Unknown parameter loc is undecided, it looks for the area on the graph with little information to place the legend9Plt.legend (labels=["$line 1$","$line 2$","$line 3$"])TenPlt.show ()

        

    

In front of us, we used plot functions to plot the line graph. Below you can draw a bar chart.

1 ImportMatplotlib.pyplot as Plt2 3Name_list = ['Monday','Tuesday','Wednesday','Thursday','Friday']4Num_list = [1.5,0.8,7.8,5,6.3]5 #the histogram drawing function is bar (), the keywords specify the number, the column height, the specified color, and the horizontal axis to replace the name6Plt.bar (Range (len (num_list)), num_list,color='RGB', tick_label=name_list)7Plt.show ()

        

Of course, the drawings we draw are not so shabby:

1 ImportMatplotlib.pyplot as Plt2 3Class_list = ['Class-one','Class-two','Class-three','Class-four']4Num_list = [3,5,8,6]5Num_list1 = [1,2,3,1]6x =list (range (len (num_list )))7 #here is the width of the column chart set8 9Total_width, n = 0.8, 2Tenwidth = total_width/N One  APlt.bar (x, num_list, WIDTH=WIDTH,FC ='b') - #Column Center offset distance -  forIinchRange (len (x)): theX[i] = X[i] +width -  -Plt.bar (x, num_list1, Width=width,tick_label = CLASS_LIST,FC ='R') -Plt.legend (labels=[" Boy","Girl"]) +Plt.show ()

          

The following is a pie chart drawing, an example from the Official Use guide:

1 ImportMatplotlib.pyplot as Plt2                                                                       3 #Slice labels for pie charts4Labels ='Frogs','Hogs','Dogs','Logs'                              5sizes = [15, 30, 45, 10]                                              6                                                                       7 #The 30% portion of the pie chart is separated, the gap value 0.18Explode = (0, 0.1, 0, 0)9                                                                       Ten #Specify Canvas 1, instantiate plt.subplots () OneFIG1, ax1 =plt.subplots () A                                                                        - #autopct is the specified percentage display specification, which retains one decimal place - #the last two keyword values are added shadows, and the starting angle is determined theAx1.pie (sizes, Explode=explode, Labels=labels, autopct='%1.1f%%',      -Shadow=true, startangle=90)                                    -                                                                        - #The equal aspect ratio ensures that the pie chart is drawn as a circle +Ax1.axis ('Equal')                                                      -                                                                        +Plt.show ()

As follows:

        

Matplotlib's drawing capabilities are able to meet our visualization needs. Other use methods, in the future study to consult on the line.

Python Data Analysis Toolkit (4)--matplotlib (ii)

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.