饼图importmatplotlib.pyplotasplt#Piechart,wherethesliceswillbeorderedandplottedcounter-clockwise:labels='Frogs','Hogs','Dogs','Logs'sizes=[15,30,45,10]explode=(0,0.1,0,0)#onlyexplodethe2ndslice(i.e.'Hogs')fig1,ax1=plt.subplots()ax1.pie(sizes,explode=explode,labels=labels,autopct='%1.1f%%',shadow=True,startangle=90)ax1.axis('equal')#Equalaspectratioensuresthatpieisdrawnasacircle.plt.show()条形图1importnumpyasnpimportmatplotlib.pyplotaspltfrommatplotlib.tickerimportMaxNLocatorfromcollectionsimportnamedtuplen_groups=5means_men=(20,35,30,35,27)std_men=(2,3,4,1,2)means_women=(25,32,34,20,25)std_women=(3,5,2,3,3)fig,ax=plt.subplots()index=np.arange(n_groups)bar_width=0.35opacity=0.4error_config={'ecolor':'0.3'}rects1=ax.bar(index,means_men,bar_width,alpha=opacity,color='b',yerr=std_men,error_kw=error_config,label='Men')rects2=ax.bar(index+bar_width,means_women,bar_width,alpha=opacity,color='r',yerr=std_women,error_kw=error_config,label='Women')ax.set_xlabel('Group')ax.set_ylabel('Scores')ax.set_title('Scoresbygroupandgender')ax.set_xticks(index+bar_width/2)ax.set_xticklabels(('A','B','C','D','E'))ax.legend()fig.tight_layout()plt.show()表格图importnumpyasnpimportmatplotlib.pyplotaspltdata=[[66386,174296,75131,577908,32015],[58230,381139,78045,99308,160454],[89135,80552,152558,497981,603535],[78415,81858,150656,193263,69638],[139361,331509,343164,781380,52269]]columns=('Freeze','Wind','Flood','Quake','Hail')rows=['%dyear'%xforxin(100,50,20,10,5)]values=np.arange(0,2500,500)value_increment=1000#Getsomepastelshadesforthecolorscolors=plt.cm.BuPu(np.linspace(0,0.5,len(rows)))n_rows=len(data)index=np.arange(len(columns))+0.3bar_width=0.4#Initializethevertical-offsetforthestackedbarchart.y_offset=np.zeros(len(columns))#Plotbarsandcreatetextlabelsforthetablecell_text=[]forrowinrange(n_rows):plt.bar(index,data[row],bar_width,bottom=y_offset,color=colors[row])y_offset=y_offset+data[row]cell_text.append(['%1.1f'%(x/1000.0)forxiny_offset])#Reversecolorsandtextlabelstodisplaythelastvalueatthetop.colors=colors[::-1]cell_text.reverse()#Addatableatthebottomoftheaxesthe_table=plt.table(cellText=cell_text,rowLabels=rows,rowColours=colors,colLabels=columns,loc='bottom')#Adjustlayouttomakeroomforthetable:plt.subplots_adjust(left=0.2,bottom=0.2)plt.ylabel(Lossin${0}'s.format(value_increment))plt.yticks(values*value_increment,['%d'%valforvalinvalues])plt.xticks([])plt.title('LossbyDisaster')plt.show()散点图importnumpyasnpimportmatplotlib.pyplotaspltimportmatplotlib.cbookascbook#Loadanumpyrecordarrayfromyahoocsvdatawithfieldsdate,open,close,#volume,adj_closefromthempl-data/exampledirectory.Therecordarray#storesthedateasannp.datetime64withadayunit('D')inthedatecolumn.withcbook.get_sample_data('goog.npz')asdatafile:price_data=np.load(datafile)['price_data'].view(np.recarray)price_data=price_data[-250:]#getthemostrecent250tradingdaysdelta1=np.diff(price_data.adj_close)/price_data.adj_close[:-1]#Markersizeinunitsofpoints^2volume=(15*price_data.volume[:-2]/price_data.volume[0])**2close=0.003*price_data.close[:-2]/0.003*price_data.open[:-2]fig,ax=plt.subplots()ax.scatter(delta1[:-1],delta1[1:],c=close,s=volume,alpha=0.5)ax.set_xlabel(r'$\Delta_i$',fontsize=15)ax.set_ylabel(r'$\Delta_{i+1}$',fontsize=15)ax.set_title('Volumeandpercentchange')ax.grid(True)fig.tight_layout()plt.show()平滑图importnumpyasnpimportmatplotlib.pyplotaspltfrommatplotlib.widgetsimportSlider,Button,RadioButtonsfig,ax=plt.subplots()plt.subplots_adjust(left=0.25,bottom=0.25)t=np.arange(0.0,1.0,0.001)a0=5f0=3delta_f=5.0s=a0*np.sin(2*np.pi*f0*t)l,=plt.plot(t,s,lw=2,color='red')plt.axis([0,1,-10,10])axcolor='lightgoldenrodyellow'axfreq=plt.axes([0.25,0.1,0.65,0.03],facecolor=axcolor)axamp=plt.axes([0.25,0.15,0.65,0.03],facecolor=axcolor)sfreq=Slider(axfreq,'Freq',0.1,30.0,valinit=f0,valstep=delta_f)samp=Slider(axamp,'Amp',0.1,10.0,valinit=a0)defupdate(val):amp=samp.valfreq=sfreq.vall.set_ydata(amp*np.sin(2*np.pi*freq*t))fig.canvas.draw_idle()sfreq.on_changed(update)samp.on_changed(update)resetax=plt.axes([0.8,0.025,0.1,0.04])button=Button(resetax,'Reset',color=axcolor,hovercolor='0.975')defreset(event):sfreq.reset()samp.reset()button.on_clicked(reset)rax=plt.axes([0.025,0.5,0.15,0.15],facecolor=axcolor)radio=RadioButtons(rax,('red','blue','green'),active=0)defcolorfunc(label):l.set_color(label)fig.canvas.draw_idle()radio.on_clicked(colorfunc)plt.show()数据打钩标签图importdatetimeimportnumpyasnpimportmatplotlib.pyplotaspltimportmatplotlib.datesasmdatesimportmatplotlib.cbookascbookyears=mdates.YearLocator()#everyyearmonths=mdates.MonthLocator()#everymonthyearsFmt=mdates.DateFormatter('%Y')#Loadanumpyrecordarrayfromyahoocsvdatawithfieldsdate,open,close,#volume,adj_closefromthempl-data/exampledirectory.Therecordarray#storesthedateasannp.datetime64withadayunit('D')inthedatecolumn.withcbook.get_sample_data('goog.npz')asdatafile:r=np.load(datafile)['price_data'].view(np.recarray)fig,ax=plt.subplots()ax.plot(r.date,r.adj_close)#formattheticksax.xaxis.set_major_locator(years)ax.xaxis.set_major_formatter(ye