Line chart Drawing:
Import Pandas as Pdunrate = Pd.read_csv (' unrate.csv ') unrate[' date ' = Pd.to_datetime (unrate[' date ') #可将1948/1/ 1 time format conversion to 1948-01-01print (Unrate.head (12))
Results:
DATE VALUE0 1948-01-01 3.41 1948-02-01 3.82 1948-03-01 4.03 1948-04-01 3.94 1948-05-01 3.55 1948-06-01 3.66 1948-07-01 3.67 1948-08-01 3.98 1948-09-01 3.89 1948-10-01 3.710 1948-11-01 3.811 1948-12-01 4.0
View Code
Import Matplotlib.pyplot as Plt#%matplotlib inline#using the different pyplot functions, we can create, customize, and Dis Play a plot. For example, we can use 2 functions to:p Lt.plot () plt.show ()
Results:
First_twelve = Unrate[0:12]plt.plot (first_twelve[' DATE '), first_twelve[' VALUE ']) plt.show ()
Results:
#While The y-axis looks fine, the x-axis tick labels is too close together and is unreadable#we can rotate the x-axis ti CK labels by degrees so they don ' t overlap#we can specify degrees of rotation using a float or integer value.plt.plot (f irst_twelve[' DATE ', first_twelve[' VALUE ']) plt.xticks (rotation=45) #指定x轴标注的角度, chosen here for 45 degrees #print Help (Plt.xticks) Plt.show ()
Results:
#xlabel (): Accepts a string value, which gets set as the x-axis label. #ylabel (): Accepts a string value, which is set as T He y-axis label. #title (): Accepts a string value, which is set as the plot title.plt.plot (first_twelve[' DATE '), First_twel ve[' VALUE ']) plt.xticks (rotation=90) plt.xlabel (' Month ') plt.ylabel (' unemployment rate ') plt.title (' Monthly Unemployment Trends, 1948 ') Plt.show ()
Results:
Sub-chart operations:
#add_subplot (First,second,index) first means number of Row,second means number of Column.import Matplotlib.pyplot as Pltfi g = Plt.figure () #规定画图区间 (paint field) Ax1 = Fig.add_subplot (3,2,1) ax2 = Fig.add_subplot (3,2,2) ax3 = Fig.add_subplot (3,2,6) Plt.show ()
Results:
Import NumPy as Npfig = Plt.figure () #fig = Plt.figure (figsize= (3, 3)) #figsize指定图的长和宽ax1 = Fig.add_subplot (2,1,1) ax2 = Fig. Add_subplot (2,1,2) Ax1.plot (Np.random.randint (1,5,5), Np.arange (5)) Ax2.plot (Np.arange (Ten), Np.arange (10)) Plt.show ()
Results:
unrate[' Month ' = unrate[' date '].dt.monthunrate[' MONTH ' = unrate[' date '].dt.monthfig = Plt.figure (figsize= (6,3)) # Draw two lines in the same diagram Plt.plot (unrate[0:12][' MONTH ', unrate[0:12][' VALUE '), c= ' Red ') #c的值可以用小写或缩写或rgb颜色通道值也可以plt. Plot (unrate [12:24] [' MONTH '], unrate[12:24][' VALUE '], c= ' Blue ') plt.show ()
Results:
Fig = Plt.figure (figsize= (10,6)) colors = [' Red ', ' blue ', ' green ', ' orange ', ' black ']for I in range (5): Start_index = i *12 End_index = (i+1) *12 subset = Unrate[start_index:end_index] plt.plot (subset[' MONTH '), subset[' VALUE '], c=colors[i]) plt.show ()
Results:
Fig = Plt.figure (figsize= (10,6)) colors = [' Red ', ' blue ', ' green ', ' orange ', ' black ']for I in range (5): Start_index = i *12 End_index = (i+1) *12 subset = unrate[start_index:end_index] label = STR (1948 + i) Plt.plot (subset[ ' MONTH '], subset[' VALUE '], c=colors[i], Label=label) plt.legend (loc= ' best ') #显示label Loc refers to the place where the label is placed, Best is automatically selected for optimal location #print Help (Plt.legend) plt.show ()
Results:
Fig = Plt.figure (figsize= (10,6)) colors = [' Red ', ' blue ', ' green ', ' orange ', ' black ']for I in range (5): Start_index = i *12 End_index = (i+1) *12 subset = unrate[start_index:end_index] label = STR (1948 + i) Plt.plot (subset[ ' Month '], subset[' VALUE '], c=colors[i], Label=label) plt.legend (loc= ' upper left ') Plt.xlabel (' month, Integer ') Plt.ylabel (' unemployment rate, Percent ') plt.title (' Monthly unemployment Trends, 1948-1952 ') plt.show ()
Results:
Bar graphs and scatter plots:
Python Data Visualization Library-matplotlib