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)