A few words of nagging:
In the near future to do data analysis, need to do visual processing of data, that is, drawing, is usually done with Matlib, but matlib installation file is too large, do not want to use it directly, it is said that its code running efficiency is very low, in the Internet can first use Java to do data processing, and then call Matlib to draw, in addition , you can also use Matplotlib, which is written in Python similar to the Matlib library, can achieve matlib function, and the quality of the drawing is very high, can be used for the publication of the paper. Looking for a day of information, finally out of the picture.
Matplotlib need to cooperate with numpy,scipy to use, the specific installation steps will be added later.
Body:
The simplest way to draw a two-dimensional image with Matplotlib is:
1) Import Matplotlib sub-modules
import Matplotlib.pyplot as Plt
2) give x, y two arrays [python list], note that the number of elements in the two list must be the same, or you will get an error
x=[1,2,3,4,5,6,7]y=[2.6,3.6,8.3,56,12.7,8.9,5.3]
3) Call the drawing method of the Pyplot module to draw the image, the basic drawing method is: plot (connect each point to a graph), scatter (scatter plot), there are more ways
Plt.plot (x, y) plt.scatter (x, y)
4) Call Pyplot's Show method to display the result.
Plt.show ()
Basic code:
Import Matplotlib.pyplot as Plt def plot2d (): x=[1,2,3,4,5,6,7] y=[2.6,3.6,8.3,56,12.7,8.9,5.3] plt.plot (x, y) # Draw line graph plt.scatter (x, y)# scatter plot plt.show () if__name__= ='__main__': plot2d ()
Result diagram:
In order: Only the plot method is called, only the scatter method is called, and the plot and scatter methods are called simultaneously
A comprehensive example is given below
This example uses Python to read the contents of the data in a. txt file (taxi location information from San Francisco, from http://crawdad.org/epfl/mobility/20090224/), as a data source for drawing images. Call the Pyplot sub-module of the Matplotlib module to draw the image. Image types include scatter plots, graphs, including legend descriptions, title, axis descriptions
Code:
#-*-CODING:GBK-*-ImportReImportMatplotlib.pyplot as PltdefLoadData (DATAFILE,ROWLIMTS):#datafile=r ' E:\cabspottingdata\new_abboip.txt 'Myfile=open (DataFile,'R', 2048)#2048 for buffer sizeNewline=myfile.readline () geopoints=[] Splitter=re.compile ('\\s')#use blank characters as separatorsRows=0 whileNewLine androws<rowlimts:content=splitter.split (newline) geopoint=[] geopoint.append (float (content[1]) geopoint.append (float (content[0)) geopoints.append (geopoint) rows+=1NewLine=myfile.readline () myfile.close ( )returngeopointsdefMain (): Rowlimits=25000#number of rows to read dataDatafile=r'E:\cabspottingdata\new_abboip.txt'DataFile2=r'E:\cabspottingdata\new_utvohovy.txt'DataFile3=r'E:\cabspottingdata\new_uvjova.txt'geopoints=LoadData (datafile,rowlimits) geopoints2=LoadData (datafile2,rowlimits) geopoints3=LoadData (datafile3,rowlimits) x=[] y=[] X2=[] y2=[] X3=[] Y3=[] forPointinchgeopoints:x.append (point[0]) y.append (point[1]) forPointinchgeopoints2:x2.append (point[0]) y2.append (point[1]) forPointinchgeopoints3:x3.append (point[0]) y3.append (point[1]) Plt.plot (X,y,color='Green', label='Cab 1')#Draw Green lines, label to function, must be used in conjunction with the Legend () methodPlt.scatter (x2,y2,color='Green', label='Cab 2')#draw a blue scatter plot with the X3,y3 list, the legend is cab 2Plt.plot (x3,y3,color='Red', label='Cab 3', linewidth=1)#draw a red line with the X3,y3 list, the legend is Cab 3, the line thickness is set to 1 #plt.scatter (x2,y2,color= ' g ', label= ' line One ', Linewidths=1) #plt.plot (x, y, ' b^ ', x2, y2, ' g ')plt.legend () Plt.grid (None,'Major','both')#draw a grid backgroundPlt.title ('plotting the trajectory of cabs in San Fransisco with%d spatial records'%rowlimits)#Set TitlePlt.xlabel ('Longitude')#Description x axis indicates longitudePlt.ylabel ('Latitude')#Description Y-axis represents latitudePlt.show ()#Show result Graph if __name__=='__main__': Main ()
A few:
Drawing two-dimensional images with matplotlib