在使用Python繪制圖表前,我們需要先安裝兩個(gè)庫(kù)文件numpy和matplotlib。
Numpy是Python開源的數(shù)值計(jì)算擴(kuò)展,可用來存儲(chǔ)和處理大型矩陣,比Python自身數(shù)據(jù)結(jié)構(gòu)要高效;matplotlib是一個(gè)Python的圖像框架,使用其繪制出來的圖形效果和MATLAB下繪制的圖形類似。
下面我通過一些簡(jiǎn)單的代碼介紹如何使用 Python繪圖。
一、圖形繪制

直方圖
importmatplotlib.pyplotaspltimportnumpyasnpmu=100sigma=20x=mu+sigma*np.random.randn(20000)# 樣本數(shù)量plt.hist(x,bins=100,color='green',normed=True)# bins顯示有幾個(gè)直方,normed是否對(duì)數(shù)據(jù)進(jìn)行標(biāo)準(zhǔn)化plt.show()
條形圖
importmatplotlib.pyplotaspltimportnumpyasnpy=[20,10,30,25,15]index=np.arange(5)plt.bar(left=index,height=y,color='green',width=0.5)plt.show()
折線圖
importmatplotlib.pyplotaspltimportnumpyasnpx=np.linspace(-10,10,100)y=x**3plt.plot(x,y,linestyle='--',color='green',marker='<')plt.show()
散點(diǎn)圖
importmatplotlib.pyplotaspltimportnumpyasnpx=np.random.randn(1000)y=x+np.random.randn(1000)*0.5plt.scatter(x,y,s=5,marker='<')# s表示面積,marker表示圖形plt.show()
餅狀圖
importmatplotlib.pyplotaspltimportnumpyasnplabels='A','B','C','D'fracs=[15,30,45,10]plt.axes(aspect=1)#使x y軸比例相同explode=[0,0.05,0,0]# 突出某一部分區(qū)域plt.pie(x=fracs,labels=labels,autopct='%.0f%%',explode=explode)#autopct顯示百分比plt.show()
箱形圖
主要用于顯示數(shù)據(jù)的分散情況。圖形分為上邊緣、上四分位數(shù)、中位數(shù)、下四分位數(shù)、下邊緣。外面的點(diǎn)時(shí)異常值
importmatplotlib.pyplotaspltimportnumpyasnpnp.random.seed(100)data=np.random.normal(size=(1000,4),loc=0,scale=1)labels=['A','B','C','D']plt.boxplot(data,labels=labels)plt.show()
二、圖像的調(diào)整
1、23種點(diǎn)形狀
"."point","pixel"o"circle"v"triangle_down"^"triangle_up"<"triangle_left">"triangle_right"1"tri_down"2"tri_up"3"tri_left"4"tri_right"8"octagon"s"square"p"pentagon"*"star"h"hexagon1"H"hexagon2"+"plus"x"x"D"diamond"d"thin_diamond
2、8種 主站蜘蛛池模板: 兴城市| 佛教| 巴东县| 三明市| 泰州市| 荣昌县| 姚安县| 罗江县| 长治县| 长寿区| 安乡县| 丰台区| 镇江市| 张家港市| 当雄县| 乌什县| 孝昌县| 安顺市| 合水县| 红桥区| 普兰县| 北流市| 资溪县| 陵川县| 临沂市| 龙游县| 宽甸| 武胜县| 孟州市| 霍林郭勒市| 浙江省| 江永县| 闻喜县| 延川县| 嘉鱼县| 徐水县| 内江市| 涞源县| 丹阳市| 通州区| 德保县|