First, Pandas import data &matplotlib basic drawing
# _*_ Coding:utf-8 _*_ Import pandas as PD import Matplotlib.pyplot as PLT # # Author:yz # date:2017-12-3 # ' Pand As import data and DATE format conversion matplotlib Basic drawing: Horizontal ordinate label, caption, coordinate value rotation, etc. ' # import data Unrate = Pd.read_csv ("data/unrate.csv") unrate["DATE" = PD. To_datetime (unrate["Date"]) # 1948/1/1-> 1948-01-01 # Print (unrate.head) ' ' Date VALUE 0 1948-01-01 3.4 1 1948-02-01 3.8 2 1948-03-01 4.0 3 1948-04-01 3.9 4 1948-05-01 3.5 5 1948-06-01 3.6 6 1948-07-01 3. 6 7 1948-08-01 3.9 8 1948-09-01 3.8 9 1948-10-01 3.7 ' ' # Plt.plot () # plt.show () ' While the Y-axis looks f INE, the x-axis tick labels are too close together and are unreadable We can rotate the x-axis tick labels by degrees s
o they don ' t overlap We can specify degrees of rotation using a float or integer value. ' # first_twelve = unrate[0:12] # plt.plot (first_twelve["DATE"], first_twelve["VALUE"]) # plt.xticks (rotation=45) # x coordinates The value is too long to rotate and then display # # print (Help (Plt.xticks)) # plt.show () '' Xlabel (): Accepts a string value, which gets set as the x-axis label.
Ylabel (): Accepts a string value, which is set as the Y-axis label.
Title (): Accepts a string value, which is set as the plot title.
' First_twelve = Unrate[0:12] Plt.plot (first_twelve[' DATE ', first_twelve[' VALUE ') plt.xticks (rotation=45) Plt.xlabel (' Month ') plt.ylabel (' Unemployment Rate ') plt.title (' monthly unemployment, Trends ') 1948 ()
Second, add the child graph & Specify image size and line color & Add label and specify location
# _*_ Coding:utf-8 _*_ import matplotlib.pyplot as PLT Import pandas as PD import NumPy as NP # Author:yz # date:20 17-12-3 # ' Add Add_subplot (first,second,index) Specifies the image size Plt.figure (figsize= (12, 6)) specifies the color of the line Plt.plot (unrate[0:12][" MONTH "], unrate[0:12][" value "], c=" red ") Add tags and specify location plt.plot (subset[" MONTH "], subset[" value "], C=colors[i], label= Label) plt.legend (loc= ' upper left ') ' "' Add_subplot (First,second,index) A/means number of Row,second means Numbe
R of Column. "# fig = plt.figure () # ax1 = Fig.add_subplot (3, 2, 1) # ax2 = Fig.add_subplot (3, 2, 2) # ax3 = Fig.add_subplot (3, 2, 3 # ax6 = Fig.add_subplot (3, 2, 6) # Ax1.plot (Np.random.randint (1, 5, 5), Np.arange (5)) # Ax2.plot (np.arange) * 3, NP.A Range () # plt.show () ' ' Specify color and size ' unrate = pd.read_csv (' data/unrate.csv ') unrate[' DATE ' = Pd.to_datetime (unrate[" Date "]) unrate[" MONTH "= unrate[" DATE "].dt.month # fig = plt.figure (figsize= (6)) # Figsize Image size # Plt.plot (unrate[ 0:12]["MONTH"], unrate[0: 12]["value"], c= "Red") # C to specify line Color # plt.plot (unrate[12:24]["MONTH"], unrate[12:24]["value"], c= "green") # plt.show () '
' lable ' fig = plt.figure (figsize= (6)) colors = [' Red ', ' blue ', ' green ', ' orange ', ' black '] for I in range (5):
Start_index = i * End_index = (i + 1) * label = STR (1948 + i) subset = Unrate[start_index:end_index] Plt.plot (subset["MONTH"], subset["VALUE"], C=colors[i], Label=label) # Add lable # Specify the location of lable ' best ' upper Right/left ' ' Lower Right/left ' right ' center Right/left ' upper/lower Center ' center ' plt.legend (loc= ' upper left ') # print (pl t.legend)) Plt.xlabel (' Month, Integer ') plt.ylabel (' Unemployment Rate, Percent ') plt.title (' monthly unemployment Trends, 1948-1952 ') plt.show ()
c. Histogram & Scatter Chart
# _*_ Coding:utf-8 _*_ Import pandas as PD import Matplotlib.pyplot as PLT from numpy import Arange # # Author:yz # Da Te:2017-12-3 # ' Columnar fig, ax = plt.subplots () ax.bar (bar_positions, bar_heights, 0.5) Scatter graph ' ' # # import data # reviews = P D.read_csv ("data/fandango_scores.csv") cols = [' FILM ', ' rt_user_norm ', ' metacritic_user_nom ', ' imdb_norm ', ' Fandango_ ' Ratingvalue ', ' fandango_stars '] norm_reviews = reviews[cols] # print (norm_reviews[:1)) # bar Chart # num_cols = [' Rt_user_n Orm ', ' metacritic_user_nom ', ' imdb_norm ', ' fandango_ratingvalue ', ' Fandango_stars '] # bar_heights = norm_reviews.ix[0, Num_cols].values # Print (bar_heights) # [4.2999999999999998 3.5499999999999998 3.8999999999999999 4.5 5.0] # Bar_positi ONS = Arange (5) + 0.75 # print (bar_positions) # [1 2 3 4 5] # fig, ax = plt.subplots () # Ax.bar (Bar_positions, bar_he Ights, 0.5) # plt.show () # Change the coordinate value to label ax.set_xticklabels (Num_cols, rotation=45) # num_cols = [' Rt_user_norm ', ' Metacrit Ic_user_nom ', ' imdb_norm ', ' FandAngo_ratingvalue ', ' Fandango_stars '] # bar_heights = norm_reviews.ix[0, num_cols].values # Print (bar_heights) # [4.2999 999999999998 3.5499999999999998 3.8999999999999999 4.5 5.0] # bar_positions = Arange (5) + 0.75 # print (bar_positions) # [1 2 3 4 5] # fig, ax = plt.subplots () # Ax.bar (bar_positions, Bar_heights, 0.5) # tick_positions = Range (1,6) # AX.S Et_xticks (tick_positions) # ax.set_xticklabels (Num_cols, rotation=45) # Ax.set_xlabel (' Rating Source ') # ax.set_ Ylabel (' Average Rating ') # ax.set_title (' Average User Rating for Avengers:age of Ultron (2015) ') # plt.show () # fig, ax = Plt.subplots () # ax.hist (norm_reviews[' Fandango_ratingvalue ']) # ax.hist (norm_reviews[' Fandango_ratingvalue '), BINS=20) # ax.hist (norm_reviews[' Fandango_ratingvalue '), Range= (3, 5), bins=20) # plt.show () # Scatter plot # fig, ax = PLT.SUBP Lots () ax.scatter (norm_reviews[' Fandango_ratingvalue '), norm_reviews[' Rt_user_norm ']) # ax.scatter ([4.5, 3], [4.3, 4] ) # (4.5, 4.3) (3, 4) Ax.set_xlabel (' FanDango ') Ax.set_ylabel (' Rotten Tomatoes ') plt.show ()
setting of graphs & coordinates
# _*_ Coding:utf-8 _*_ Import pandas as PD import Matplotlib.pyplot as PLT # # Author:yz # date:2017-12-3 # ' curve "' Women_degrees = pd.read_csv (' data/percent-bachelors-degrees-women-usa.csv ') # Plt.plot (women_degrees[' year '), women_degrees[' Biology '] # plt.show () # Plt.plot (women_degrees[' year '), women_degrees[' Biology '], c= ' Blue ', label= ' Women ') # Plt.plot (women_degrees[' year '), 100-women_degrees[' Biology '], c= ' green ', label= ' Men ') # plt.legend (loc= ' Upper right '] # plt.title (' Percentage of biology Degrees awarded by Gender ') # plt.show () # fig, ax = plt.subplots () # Ax.plot (women_degrees[' year '], women_degrees[' biology '], label= ' Women ') # Ax.plot (women_degrees[' year '), 100-women_ degrees[' Biology '], label= ' Men ') # Ax.tick_params (bottom= "Off", top= "off", left= "off", right= "Off") # Ax.set_title (' Percentage of biology Degrees awarded by Gender ') # ax.legend (loc= "upper Right") # # Plt.show () # fig, ax = plt.subplots () # Ax.plot (women_degrees[' year '), women_degrees[' BioLogy '], c= ' Blue ', label= ' Women ') # Ax.plot (women_degrees[' year '), 100-women_degrees[' Biology '], c= ' green ', label= ' Men ') # Ax.tick_params (bottom= "Off", top= "off", left= "off", right= "Off") # for Key,spine in Ax.spines.items (): # SPI
Ne.set_visible (False) # # End Solution code. # ax.legend (loc= ' upper right ') # plt.show () major_cats = [' Biology ', ' Computer science ', ' Engineering ', ' Math and Statis Tics '] Fig = plt.figure (figsize=) for SP in range (0,4): Ax = Fig.add_subplot (2,2,sp+1) Ax.plot (Women_degr ees[' year '], WOMEN_DEGREES[MAJOR_CATS[SP]], c= ' Blue ', label= ' Women ') ax.plot (women_degrees[' year '), 100-women_
DEGREES[MAJOR_CATS[SP]], c= ' green ', label= ' Men ') # ADD your code here.
# calling Pyplot.legend () here'll add the legend to the last subplot this was created. Plt.legend (loc= ' upper right ") plt.show () major_cats = [' Biology ', ' Computer science ', ' Engineering ', ' Math and Statistics '] Fig = plt.figure (figsize=) for SP in range (0,4): Ax = Fig.Add_subplot (2,2,sp+1) ax.plot (women_degrees[' year ', Women_degrees[major_cats[sp]], c= ' Blue ', label= ' women ') ax.pl OT (women_degrees[' year '], 100-WOMEN_DEGREES[MAJOR_CATS[SP]], c= ' green ', label= ' Men ') for Key,spine in Ax.spines.items
(): Spine.set_visible (False) Ax.set_xlim (1968) Ax.set_ylim (0,100) ax.set_title (MAJOR_CATS[SP)) Ax.tick_params (bottom= "Off", top= ' off ', left= ' off ', right= ' off ') # calling Pyplot.legend () here'll add the legend to
The last subplot is created. Plt.legend (loc= ' upper right ') plt.show ()
type of line
# _*_ Coding:utf-8 _*_ Import pandas as PD import Matplotlib.pyplot as PLT # author:yz # date:2017-12-3 # Women_d Egrees = pd.read_csv (' data/percent-bachelors-degrees-women-usa.csv ') major_cats = [' Biology ', ' Computer science ', '
Engineering ', ' Math and Statistics '] # Cb_dark_blue = (0/255, 107/255, 164/255) # Cb_orange = (255/255, 128/255, 14/255) # fig = Plt.figure (figsize=) # for SP in range (0,4): # ax = Fig.add_subplot (2,2,sp+1) # The color F
or each of the line are assigned here. # Ax.plot (women_degrees[' year ', Women_degrees[major_cats[sp]], c=cb_dark_blue, label= ' Women ') # Ax.plot (women_deg rees[' year '], 100-WOMEN_DEGREES[MAJOR_CATS[SP]], C=cb_orange, label= ' Men ') # for Key,spine in Ax.spines.items (): # Spine.set_visible (False) # Ax.set_xlim (1968) # Ax.set_ylim (0,100) # Ax.set_title (MAJOR_CATS[SP)) # Ax.tick_params (bottom= "Off", top= "off", left= "off", right= "Off") # Plt.legend (loc= ' upper right ') # plt.show () #SetTing line Width Cb_dark_blue = (0/255, 107/255, 164/255) Cb_orange = (255/255, 128/255, 14/255) FIG = plt.figure (figsize=
(12, 12)) For SP in range (0,4): Ax = Fig.add_subplot (2,2,sp+1) # Set "line width" specifying how to each line should loo K. Ax.plot (women_degrees[' year ', Women_degrees[major_cats[sp]], c=cb_dark_blue, label= ' women ', linewidth=10) AX.P Lot (women_degrees[' year ', 100-women_degrees[major_cats[sp]], C=cb_orange, label= ' Men ', linewidth=10) for Key,spine I n Ax.spines.items (): spine.set_visible (False) Ax.set_xlim (1968,) Ax.set_ylim (0,100) Ax.set_title ( MAJOR_CATS[SP]) ax.tick_params (bottom= "Off", top= "off", left= "off", right= "Off") plt.legend (loc= ' upper right ') plt.sh OW ()