How the data is clear, accurate, interactive, and visualized through data, will achieve these effects.
Libraries needed for Python visualization: pandas,matplotlib
Refer to the official tutorial: http://matplotlib.org/index.html
Scatter plot:
Plot function: Plot (x, Y, '. ', Color (r,g,b))
X, y,x axis and y-axis sequence; '. ', the size of the midpoint of the scatter plot; Color:rgb definition
#-*-coding:utf-8-*-ImportPandasImportmatplotlibImportMatplotlib.pyplot as Pltdata=Pandas.read_csv ('C://users//leon//desktop//data.csv') Maincolor= (52/256, 88/256, 151/256, 1)#1 is transparencyFont= { 'size': 20, 'Family':'Simhei'} #sets the font and does not display the Chinese font if not setMatplotlib.rc ('Font', **font)#%matplotlib qt#Plt.grid (True)#Little DotPlt.xlabel ('Advertising costs', color=maincolor) Plt.ylabel ('number of users purchased', color=maincolor) plt.tick_params (axis='x', Colors=maincolor)#tick mark SettingsPlt.tick_params (axis='y', colors=maincolor) Plt.plot (data['Advertising costs'], data['number of users purchased'], '.', color=Maincolor)
If the running environment is pycharm, add it at the end:
Plt.show ()
Before you can display graphics.
Line chart:
Plot (X,y,style,color,linewidth)
Title (' Plotting of the graph ')
Style, the pattern of a line; the width of a linewidth line.
ImportPandasImportmatplotlib fromMatplotlibImportPyplot as Pltdata=Pandas.read_csv ('C://users//leon//desktop//data.csv')#Converting a date formatdata['Date of purchase'] =Pandas.to_datetime (data['Date']) Maincolor= (42/256, 87/256, 141/256, 1); font= { 'size': 20, 'Family':'Simhei'}matplotlib.rc ('Font', **font)#%matplotlib qtPlt.xlabel ('Date of purchase', Color=maincolor) Plt.ylabel ('number of users purchased', Color=maincolor) plt.tick_params (axis='x', Colors=maincolor) plt.tick_params (axis='y', Colors=Maincolor)#'-' a smooth curvePlt.plot (data['Date of purchase'], data['number of users purchased'], '-', color=maincolor) Plt.title ('number of users purchased') plt.show ()
The Plt.plot (the third parameter is set to '-', as opposed to the scatter plot, can be converted to the line graph.)
Pie chart:
Pie (x,labels,colors,explode,autopct)
X-plot sequence, labels tag sequence, colors color, explode need to highlight the sequence, autopct pie Proportion of the hand format;%.2f reserved 2 decimal places
Labels ='Frogs','Hogs','Dogs','Logs'Sizes= [15, 30, 45, 10]explode= (0, 0.1, 0, 0)#Only "Explode" the 2nd slice (i.e. ' hogs ')Fig1, Ax1=plt.subplots () Ax1.pie (sizes, explode=explode, Labels=labels, autopct='%1.1f%%', Shadow=true, startangle=90) Ax1.axis ('Equal')#Equal aspect ratio ensures that pie is drawn as a circle.
#设置为横轴和纵轴等长的饼图
#也就是圆形的饼图, not an oval pie chart
Plt.show ()
Column chart:
Bar (Left,height,width,color)
Left x-axis sequence; height y-axis value; width column chart widths; color Fill Colors
ImportNumPyImportPandasImportmatplotlib fromMatplotlibImportPyplot as Pltfont= { 'Family':'Simhei'}matplotlib.rc ('Font', **font)#Create a Chinese environmentData=Pandas.read_csv ('C://users//leon//desktop//data.csv') Result=Data.groupby ( by=['Mobile Brand'], As_index=False) ['monthly Consumption (yuan)'].agg ({'Monthly Consumption': Numpy.sum}) #Perspective#Vertical Column ChartMaincolor = (42/256, 87/256, 141/256, 1) Index=numpy.arange (result. Monthly consumption. Size) SGB=Result.sort_values ( by="Monthly Consumption", Ascending=False)#Descending, False to True is ascendingPlt.bar (Index, SGB. Monthly consumption, color=maincolor) plt.xticks (index, SGB. Mobile brand)#xticks function to add Chinese labelPlt.show ()
python--Visualization of data