1# 1 通过python的基础数据对象转化2import numpy as np
3
x =[1,2,3,4]4
x = np.array(x)5
x
Out[1]:
array([1,2,3,4])
In [2]:1type(x)
Out[2]:
numpy.ndarray
In [3]:1# 2 通过numpy内生的函数直接生成2
x = np.arange(0,11)3
x
Out[3]:
array([0,1,2,3,4,5,6,7,8,9,10])
In [4]:1# 3 从硬盘读取数据2# 参数依次是:文件名;分隔符;跳过哪几行;选取哪几列;3
x = np.loadtxt('000001.csv', delimiter =',', skiprows =1, usecols =(1,4,6), unpack =False)4
x
Out[4]:
array([[3.25863000e+03,3.35052000e+03,5.31352384e+10],[3.33080000e+03,3.35145000e+03,5.01661696e+10],[3.32665000e+03,3.37395000e+03,3.91918880e+10],[3.37196000e+03,3.29346000e+03,3.71131168e+10],[3.27697000e+03,3.28541000e+03,4.10240864e+10],[3.25821000e+03,3.22932000e+03,3.22064672e+10],[3.22354000e+03,3.23530000e+03,2.30725760e+10],[3.24234000e+03,3.22244000e+03,2.40190752e+10],[3.22407000e+03,3.33646000e+03,2.82546240e+10],[3.34360000e+03,3.37650000e+03,3.39876768e+10],[3.18973000e+03,3.11635000e+03,4.01098784e+10],[3.11456000e+03,3.17305000e+03,3.57080800e+10],[3.18909000e+03,3.32361000e+03,4.10956032e+10],[3.32732000e+03,3.34334000e+03,3.53382976e+10],[3.35710000e+03,3.35176000e+03,3.66249248e+10],[3.34726000e+03,3.38318000e+03,3.17540992e+10],[3.38985000e+03,3.35296000e+03,3.74517536e+10],[3.32572000e+03,3.30574000e+03,3.01927104e+10],[3.25900000e+03,3.26230000e+03,2.74658624e+10],[3.27375000e+03,3.21036000e+03,2.58312544e+10],[3.14814000e+03,3.12830000e+03,2.50861632e+10],[3.15609000e+03,3.20491000e+03,2.48192160e+10],[3.21282000e+03,3.17413000e+03,2.49098080e+10],[3.25121000e+03,3.13653000e+03,3.06139296e+10],[3.12009000e+03,3.07591000e+03,2.46749664e+10],[3.06351000e+03,3.09512000e+03,2.06108384e+10],[3.09049000e+03,3.14159000e+03,1.93817136e+10],[3.14576000e+03,3.15770000e+03,1.72840096e+10],[3.15796000e+03,3.17342000e+03,1.94592304e+10],[3.18681000e+03,3.20383000e+03,2.61290432e+10],[3.20614000e+03,3.22236000e+03,2.23797424e+10],[3.23088000e+03,3.24691000e+03,2.28332624e+10],[3.25648000e+03,3.22884000e+03,2.33348096e+10],[3.22215000e+03,3.29836000e+03,3.01263872e+10],[3.29683000e+03,3.31030000e+03,2.99163712e+10],[3.33272000e+03,3.33628000e+03,3.46445664e+10],[3.31770000e+03,3.26305000e+03,3.82044608e+10],[3.26418000e+03,3.27953000e+03,2.93639520e+10],[3.26409000e+03,3.24848000e+03,3.20663584e+10],[3.24804000e+03,3.24119000e+03,2.82915776e+10],[3.22431000e+03,3.30241000e+03,3.21495424e+10],[3.28909000e+03,3.28607000e+03,2.85817568e+10],[3.28959000e+03,3.29090000e+03,2.82985536e+10],[3.31481000e+03,3.34932000e+03,3.57295104e+10],[3.35949000e+03,3.37291000e+03,3.28410144e+10],[3.39116000e+03,3.44930000e+03,3.99132416e+10],[3.46960000e+03,3.50285000e+03,5.20939520e+10],[3.51050000e+03,3.57730000e+03,5.45217152e+10],[3.57602000e+03,3.58227000e+03,5.37346624e+10],[3.58708000e+03,3.61732000e+03,5.16661664e+10],[3.64010000e+03,3.68773000e+03,5.36062848e+10],[3.69257000e+03,3.69141000e+03,6.39554688e+10],[3.68095000e+03,3.66073000e+03,5.21886336e+10],[3.64194000e+03,3.68210000e+03,4.88647200e+10],[3.68613000e+03,3.69110000e+03,4.08945152e+10],[3.71061000e+03,3.78657000e+03,5.64702336e+10],[3.82299000e+03,3.74790000e+03,5.61676032e+10],[3.74834000e+03,3.81029000e+03,4.47458304e+10],[3.82769000e+03,3.82578000e+03,4.79299680e+10],[3.80338000e+03,3.86393000e+03,4.73033312e+10],[3.89942000e+03,3.96138000e+03,5.70447552e+10],[3.97653000e+03,3.99481000e+03,6.18085440e+10],[4.00613000e+03,3.95753000e+03,5.85176832e+10],[3.94749000e+03,4.03431000e+03,4.84283616e+10],[4.07272000e+03,4.12171000e+03,5.89814208e+10],[4.12578000e+03,4.13556000e+03,6.10683520e+10],[4.13565000e+03,4.08416000e+03,6.13005824e+10],[4.05592000e+03,4.19482000e+03,5.51242944e+10],[4.25472000e+03,4.28730000e+03,7.01706176e+10],[4.30135000e+03,4.21708000e+03,8.57132800e+10],[4.21219000e+03,4.29362000e+03,6.34470656e+10],[4.30460000e+03,4.39849000e+03,6.80305088e+10],[4.41448000e+03,4.41451000e+03,6.67344640e+10],[4.35595000e+03,4.39369000e+03,6.28555008e+10],[4.44193000e+03,4.52740000e+03,6.71088512e+10],[4.52763000e+03,4.47621000e+03,7.67676416e+10],[4.44612000e+03,4.47662000e+03,5.19834208e+10],[4.48301000e+03,4.44165000e+03,5.26727968e+10],[4.44134000e+03,4.48046000e+03,4.94173408e+10],[4.47985000e+03,4.29871000e+03,5.72858624e+10],[4.31164000e+03,4.22927000e+03,4.81732992e+10],[4.19790000e+03,4.11221000e+03,3.94566656e+10],[4.15298000e+03,4.20592000e+03,3.97428096e+10],[4.23127000e+03,4.33358000e+03,4.88750528e+10],[4.34237000e+03,4.40122000e+03,5.21866400e+10],[4.40238000e+03,4.37576000e+03,5.10490464e+10],[4.37282000e+03,4.37831000e+03,4.49077920e+10],[4.36682000e+03,4.30869000e+03,4.39706208e+10],[4.27790000e+03,4.28349000e+03,3.80057440e+10],[4.28578000e+03,4.41755000e+03,4.36735232e+10],[4.43498000e+03,4.44629000e+03,5.14106208e+10],[4.45644000e+03,4.52942000e+03,4.64996512e+10],[4.58498000e+03,4.65760000e+03,6.55591296e+10],[4.66008000e+03,4.81380000e+03,6.82461376e+10],[4.85485000e+03,4.91090000e+03,7.04892800e+10],[4.93285000e+03,4.94171000e+03,6.81165376e+10],[4.94374000e+03,4.62027000e+03,7.82964608e+10],[4.60346000e+03,4.61174000e+03,6.11262400e+10],[4.63310000e+03,4.82874000e+03,5.93389056e+10],[4.84470000e+03,4.91053000e+03,6.23748096e+10],[4.92438000e+03,4.90998000e+03,6.11453824e+10],[4.91294000e+03,4.94710000e+03,6.74952384e+10],[5.01609000e+03,5.02310000e+03,7.72240832e+10],[5.04569000e+03,5.13188000e+03,8.55035072e+10],[5.14598000e+03,5.11353000e+03,7.29893824e+10],[5.04920000e+03,5.10604000e+03,5.96969024e+10],[5.10144000e+03,5.12159000e+03,5.63990528e+10],[5.14334000e+03,5.16635000e+03,6.25627840e+10],[5.17442000e+03,5.06299000e+03,6.37803968e+10],[5.00441000e+03,4.88743000e+03,5.50801408e+10],[4.89055000e+03,4.96790000e+03,5.37101184e+10],[4.94252000e+03,4.78536000e+03,5.07440896e+10],[4.68993000e+03,4.47836000e+03,4.52689632e+10],[4.47161000e+03,4.57649000e+03,4.73526144e+10],[4.60458000e+03,4.69015000e+03,5.43003712e+10],[4.71176000e+03,4.52778000e+03,5.72797504e+10],[4.39993000e+03,4.19287000e+03,5.65217856e+10],[4.28977000e+03,4.05303000e+03,6.73786368e+10],[4.00675000e+03,4.27722000e+03,7.09176640e+10],[4.21415000e+03,4.05370000e+03,5.98769408e+10],[4.05862000e+03,3.91277000e+03,5.86015616e+10],[3.79371000e+03,3.68692000e+03,5.48163136e+10],[3.97521000e+03,3.77591000e+03,8.31139264e+10],[3.65478000e+03,3.72712000e+03,6.98818688e+10],[3.46740000e+03,3.50719000e+03,6.80356928e+10],[3.43245000e+03,3.70933000e+03,6.56914624e+10],[3.70746000e+03,3.87780000e+03,5.86364224e+10],[3.91899000e+03,3.97039000e+03,6.43489024e+10],[3.95837000e+03,3.92449000e+03,6.70558784e+10],[3.87497000e+03,3.80570000e+03,6.01301312e+10],[3.75850000e+03,3.82318000e+03,4.92256192e+10],[3.83142000e+03,3.95735000e+03,4.81726272e+10],[3.94842000e+03,3.99211000e+03,5.39106688e+10],[3.93990000e+03,4.01767000e+03,5.04288032e+10],[3.99643000e+03,4.02604000e+03,5.20732224e+10],[4.02227000e+03,4.12392000e+03,5.63585984e+10],[4.12475000e+03,4.07091000e+03,6.27424832e+10],[3.98557000e+03,3.72556000e+03,5.56003264e+10],[3.57314000e+03,3.66300000e+03,5.63330048e+10],[3.68982000e+03,3.78917000e+03,4.34352096e+10],[3.77379000e+03,3.70577000e+03,4.57943232e+10],[3.65567000e+03,3.66373000e+03,3.50955744e+10],[3.61499000e+03,3.62291000e+03,3.63968736e+10],[3.62186000e+03,3.75654000e+03,3.62901664e+10],[3.74565000e+03,3.69457000e+03,3.66422976e+10],[3.62550000e+03,3.66154000e+03,2.74074656e+10],[3.69261000e+03,3.74420000e+03,3.40757184e+10],[3.78603000e+03,3.92842000e+03,4.97304320e+10],[3.92881000e+03,3.92791000e+03,5.38923456e+10],[3.88123000e+03,3.88632000e+03,4.42688288e+10],[3.86991000e+03,3.95456000e+03,4.30073312e+10],[3.97641000e+03,3.96534000e+03,4.67988224e+10],[3.94784000e+03,3.99367000e+03,4.60432064e+10],[3.99913000e+03,3.74816000e+03,5.43770816e+10],[3.64680000e+03,3.79411000e+03,4.75396224e+10],[3.75457000e+03,3.66429000e+03,3.90063072e+10],[3.60996000e+03,3.50774000e+03,3.69920480e+10],[3.37348000e+03,3.20991000e+03,3.34671808e+10],[3.00413000e+03,2.96497000e+03,3.52325120e+10],[2.98079000e+03,2.92729000e+03,4.66699648e+10],[2.97803000e+03,3.08359000e+03,4.00308384e+10],[3.12526000e+03,3.23235000e+03,4.43136928e+10],[3.20356000e+03,3.20599000e+03,3.97431392e+10],[3.15783000e+03,3.16662000e+03,4.32432480e+10],[3.02768000e+03,3.16017000e+03,4.38170144e+10],[3.14938000e+03,3.08042000e+03,2.96468128e+10],[3.05444000e+03,3.17045000e+03,2.55415472e+10],[3.18255000e+03,3.24309000e+03,3.75327968e+10],[3.19055000e+03,3.19789000e+03,2.73261760e+10],[3.18948000e+03,3.20023000e+03,2.24557824e+10],[3.22117000e+03,3.11480000e+03,3.46631168e+10],[3.04380000e+03,3.00517000e+03,2.49194448e+10],[2.99804000e+03,3.15226000e+03,2.77524512e+10],[3.13198000e+03,3.08606000e+03,3.17602880e+10],[3.10028000e+03,3.09792000e+03,2.09175392e+10],[3.07209000e+03,3.15654000e+03,2.39897360e+10],[3.16132000e+03,3.18562000e+03,2.74786144e+10],[3.13772000e+03,3.11589000e+03,2.36322672e+10],[3.12649000e+03,3.14269000e+03,2.12887728e+10],[3.13085000e+03,3.09235000e+03,2.36263872e+10],[3.08557000e+03,3.10076000e+03,1.56727536e+10],[3.05522000e+03,3.03814000e+03,1.63222672e+10],[3.05284000e+03,3.05278000e+03,1.46642448e+10],[3.15607000e+03,3.14336000e+03,2.34276048e+10],[3.14664000e+03,3.18315000e+03,2.34851440e+10],[3.19354000e+03,3.28766000e+03,3.86294720e+10],[3.26216000e+03,3.29323000e+03,2.97153120e+10],[3.28002000e+03,3.26244000e+03,2.95077728e+10],[3.25503000e+03,3.33807000e+03,3.16283840e+10],[3.35830000e+03,3.39135000e+03,3.95460576e+10],[3.40163000e+03,3.38670000e+03,3.78112192e+10],[3.37755000e+03,3.42533000e+03,3.18973760e+10],[3.42856000e+03,3.32068000e+03,4.58455424e+10],[3.29229000e+03,3.36874000e+03,3.23739328e+10],[3.37755000e+03,3.41243000e+03,3.47372864e+10],[3.44865000e+03,3.42958000e+03,3.65560864e+10],[3.40914000e+03,3.43434000e+03,3.28172768e+10],[3.41701000e+03,3.37520000e+03,2.93523296e+10],[3.38777000e+03,3.38732000e+03,2.35676016e+10],[3.38028000e+03,3.38256000e+03,2.43595120e+10],[3.33758000e+03,3.32508000e+03,2.30951136e+10],[3.33032000e+03,3.31670000e+03,1.92897440e+10],[3.32562000e+03,3.45964000e+03,3.39078752e+10],[3.45922000e+03,3.52282000e+03,5.53254976e+10],[3.51444000e+03,3.59003000e+03,4.29167040e+10],[3.58850000e+03,3.64688000e+03,5.03016704e+10],[3.61740000e+03,3.64049000e+03,4.29746592e+10],[3.63500000e+03,3.65025000e+03,3.60972672e+10],[3.65682000e+03,3.63290000e+03,3.61717600e+10],[3.60076000e+03,3.58084000e+03,3.45870944e+10],[3.52246000e+03,3.60696000e+03,2.76187040e+10],[3.62998000e+03,3.60480000e+03,3.83575456e+10],[3.60506000e+03,3.56847000e+03,2.97580736e+10],[3.57378000e+03,3.61706000e+03,2.47915584e+10],[3.62079000e+03,3.63050000e+03,3.10801984e+10],[3.63087000e+03,3.61032000e+03,3.15997472e+10],[3.60289000e+03,3.61611000e+03,2.48810512e+10],[3.61407000e+03,3.64793000e+03,2.73024864e+10],[3.65957000e+03,3.63555000e+03,3.06761600e+10],[3.61654000e+03,3.43630000e+03,3.54287520e+10],[3.43386000e+03,3.44540000e+03,3.04197888e+10],[3.44244000e+03,3.45631000e+03,2.52390752e+10],[3.45028000e+03,3.53691000e+03,3.01491488e+10],[3.52573000e+03,3.58482000e+03,2.81111232e+10],[3.55815000e+03,3.52499000e+03,2.51736416e+10],[3.52981000e+03,3.53693000e+03,2.08302576e+10],[3.51865000e+03,3.47007000e+03,2.24367312e+10],[3.46258000e+03,3.47244000e+03,1.95698848e+10],[3.46981000e+03,3.45550000e+03,2.00427520e+10],[3.44160000e+03,3.43458000e+03,1.82908880e+10],[3.40351000e+03,3.52067000e+03,2.15374624e+10],[3.51813000e+03,3.51035000e+03,2.00471344e+10],[3.52209000e+03,3.51619000e+03,1.93482304e+10],[3.53363000e+03,3.58000000e+03,2.83856480e+10],[3.57494000e+03,3.57896000e+03,2.73707904e+10],[3.56858000e+03,3.64247000e+03,2.99849280e+10],[3.64599000e+03,3.65177000e+03,2.61178752e+10],[3.65328000e+03,3.63609000e+03,2.98201792e+10],[3.63131000e+03,3.61249000e+03,2.27785216e+10],[3.61405000e+03,3.62791000e+03,1.98451120e+10],[3.63577000e+03,3.53378000e+03,2.69983264e+10],[3.52840000e+03,3.51876000e+03,7.50513110e+09]])
In [6]:1
x.shape
Out[6]:(242,3)
In [7]:1# unpack=True表示把数据分成几个不同的变量存储2open, close, volume = np.loadtxt('000001.csv', delimiter =',', skiprows =1, usecols =(1,4,6), unpack =True)3open
Out[7]:
array([3258.63,3330.8,3326.65,3371.96,3276.97,3258.21,3223.54,3242.34,3224.07,3343.6,3189.73,3114.56,3189.09,3327.32,3357.1,3347.26,3389.85,3325.72,3259.,3273.75,3148.14,3156.09,3212.82,3251.21,3120.09,3063.51,3090.49,3145.76,3157.96,3186.81,3206.14,3230.88,3256.48,3222.15,3296.83,3332.72,3317.7,3264.18,3264.09,3248.04,3224.31,3289.09,3289.59,3314.81,3359.49,3391.16,3469.6,3510.5,3576.02,3587.08,3640.1,3692.57,3680.95,3641.94,3686.13,3710.61,3822.99,3748.34,3827.69,3803.38,3899.42,3976.53,4006.13,3947.49,4072.72,4125.78,4135.65,4055.92,4254.72,4301.35,4212.19,4304.6,4414.48,4355.95,4441.93,4527.63,4446.12,4483.01,4441.34,4479.85,4311.64,4197.9,4152.98,4231.27,4342.37,4402.38,4372.82,4366.82,4277.9,4285.78,4434.98,4456.44,4584.98,4660.08,4854.85,4932.85,4943.74,4603.46,4633.1,4844.7,4924.38,4912.94,5016.09,5045.69,5145.98,5049.2,5101.44,5143.34,5174.42,5004.41,4890.55,4942.52,4689.93,4471.61,4604.58,4711.76,4399.93,4289.77,4006.75,4214.15,4058.62,3793.71,3975.21,3654.78,3467.4,3432.45,3707.46,3918.99,3958.37,3874.97,3758.5,3831.42,3948.42,3939.9,3996.43,4022.27,4124.75,3985.57,3573.14,3689.82,3773.79,3655.67,3614.99,3621.86,3745.65,3625.5,3692.61,3786.03,3928.81,3881.23,3869.91,3976.41,3947.84,3999.13,3646.8,3754.57,3609.96,3373.48,3004.13,2980.79,2978.03,3125.26,3203.56,3157.83,3027.68,3149.38,3054.44,3182.55,3190.55,3189.48,3221.17,3043.8,2998.04,3131.98,3100.28,3072.09,3161.32,3137.72,3126.49,3130.85,3085.57,3055.22,3052.84,3156.07,3146.64,3193.54,3262.16,3280.02,3255.03,3358.3,3401.63,3377.55,3428.56,3292.29,3377.55,3448.65,3409.14,3417.01,3387.77,3380.28,3337.58,3330.32,3325.62,3459.22,3514.44,3588.5,3617.4,3635.,3656.82,3600.76,3522.46,3629.98,3605.06,3573.78,3620.79,3630.87,3602.89,3614.07,3659.57,3616.54,3433.86,3442.44,3450.28,3525.73,3558.15,3529.81,3518.65,3462.58,3469.81,3441.6,3403.51,3518.13,3522.09,3533.63,3574.94,3568.58,3645.99,3653.28,3631.31,3614.05,3635.77,3528.4])
索引
In [8]:1
x = np.arange(11)2
x
Out[8]:
array([0,1,2,3,4,5,6,7,8,9,10])
In [9]:1
x[::2]# 第一个冒号表示选择所有,第二个冒号表示步长
Out[9]:
array([0,2,4,6,8,10])
In [10]:1
x[::-1]# -1会倒序输出
Out[10]:
array([10,9,8,7,6,5,4,3,2,1,0])
排序
如果使用np.sort(x)排序,会生成一个新的有序数组,原x不变 如果使用x.sort()排序,会改变x的值,不会生成新数组
In [11]:1
x = np.random.randint(1,100,10)2
x
Out[11]:
array([46,54,24,22,42,13,91,10,16,14])
In [12]:1
y = np.sort(x)2
y
Out[12]:
array([10,13,14,16,22,24,42,46,54,91])
In [13]:1
x
Out[13]:
array([46,54,24,22,42,13,91,10,16,14])
In [14]:1
z = x.sort()2
z # 什么都没有输出说明没有生成新的数组
In [15]:1
x
Out[15]:
array([10,13,14,16,22,24,42,46,54,91])
In []:1
2 散点图
1import numpy as np
2import matplotlib.pyplot as plt
In [3]:1
height =[160,175,170,180,185,165]2
weight =[80,120,122,140,145,90]3
plt.scatter(height, weight)4
plt.show()
不相关
In [4]:1
n =10002
x = np.random.randn(n)3
y = np.random.randn(n)4
plt.scatter(x, y)5
plt.show()
正相关
In [5]:1
n =10002
x = np.random.randn(n)3
y = x + np.random.randn(n)*0.54
plt.scatter(x, y)5
plt.show()
负相关
In [6]:1
n =10002
x = np.random.randn(n)3
y =-x + np.random.randn(n)*0.54
plt.scatter(x, y)5
plt.show()
实例
判断股票每天开盘价与收盘价之间的差值是否与下一天有关联
In [7]:1open, close = np.loadtxt('000001.csv', delimiter =',', skiprows =1, usecols =(1,4), unpack =True)2
change = close -open3
change
Out[7]:
array([9.1890e+01,2.0650e+01,4.7300e+01,-7.8500e+01,8.4400e+00,-2.8890e+01,1.1760e+01,-1.9900e+01,1.1239e+02,3.2900e+01,-7.3380e+01,5.8490e+01,1.3452e+02,1.6020e+01,-5.3400e+00,3.5920e+01,-3.6890e+01,-1.9980e+01,3.3000e+00,-6.3390e+01,-1.9840e+01,4.8820e+01,-3.8690e+01,-1.1468e+02,-4.4180e+01,3.1610e+01,5.1100e+01,1.1940e+01,1.5460e+01,1.7020e+01,1.6220e+01,1.6030e+01,-2.7640e+01,7.6210e+01,1.3470e+01,3.5600e+00,-5.4650e+01,1.5350e+01,-1.5610e+01,-6.8500e+00,7.8100e+01,-3.0200e+00,1.3100e+00,3.4510e+01,1.3420e+01,5.8140e+01,3.3250e+01,6.6800e+01,6.2500e+00,3.0240e+01,4.7630e+01,-1.1600e+00,-2.0220e+01,4.0160e+01,4.9700e+00,7.5960e+01,-7.5090e+01,6.1950e+01,-1.9100e+00,6.0550e+01,6.1960e+01,1.8280e+01,-4.8600e+01,8.6820e+01,4.8990e+01,9.7800e+00,-5.1490e+01,1.3890e+02,3.2580e+01,-8.4270e+01,8.1430e+01,9.3890e+01,3.0000e-02,3.7740e+01,8.5470e+01,-5.1420e+01,3.0500e+01,-4.1360e+01,3.9120e+01,-1.8114e+02,-8.2370e+01,-8.5690e+01,5.2940e+01,1.0231e+02,5.8850e+01,-2.6620e+01,5.4900e+00,-5.8130e+01,5.5900e+00,1.3177e+02,1.1310e+01,7.2980e+01,7.2620e+01,1.5372e+02,5.6050e+01,8.8600e+00,-3.2347e+02,8.2800e+00,1.9564e+02,6.5830e+01,-1.4400e+01,3.4160e+01,7.0100e+00,8.6190e+01,-3.2450e+01,5.6840e+01,2.0150e+01,2.3010e+01,-1.1143e+02,-1.1698e+02,7.7350e+01,-1.5716e+02,-2.1157e+02,1.0488e+02,8.5570e+01,-1.8398e+02,-2.0706e+02,-2.3674e+02,2.7047e+02,-1.6045e+02,-1.4585e+02,-1.0679e+02,-1.9930e+02,7.2340e+01,3.9790e+01,2.7688e+02,1.7034e+02,5.1400e+01,-3.3880e+01,-6.9270e+01,6.4680e+01,1.2593e+02,4.3690e+01,7.7770e+01,2.9610e+01,1.0165e+02,-5.3840e+01,-2.6001e+02,8.9860e+01,9.9350e+01,-6.8020e+01,8.0600e+00,7.9200e+00,1.3468e+02,-5.1080e+01,3.6040e+01,5.1590e+01,1.4239e+02,-9.0000e-01,5.0900e+00,8.4650e+01,-1.1070e+01,4.5830e+01,-2.5097e+02,1.4731e+02,-9.0280e+01,-1.0222e+02,-1.6357e+02,-3.9160e+01,-5.3500e+01,1.0556e+02,1.0709e+02,2.4300e+00,8.7900e+00,1.3249e+02,-6.8960e+01,1.1601e+02,6.0540e+01,7.3400e+00,1.0750e+01,-1.0637e+02,-3.8630e+01,1.5422e+02,-4.5920e+01,-2.3600e+00,8.4450e+01,2.4300e+01,-2.1830e+01,1.6200e+01,-3.8500e+01,1.5190e+01,-1.7080e+01,-6.0000e-02,-1.2710e+01,3.6510e+01,9.4120e+01,3.1070e+01,-1.7580e+01,8.3040e+01,3.3050e+01,-1.4930e+01,4.7780e+01,-1.0788e+02,7.6450e+01,3.4880e+01,-1.9070e+01,2.5200e+01,-4.1810e+01,-4.5000e-01,2.2800e+00,-1.2500e+01,-1.3620e+01,1.3402e+02,6.3600e+01,7.5590e+01,5.8380e+01,2.3090e+01,1.5250e+01,-2.3920e+01,-1.9920e+01,8.4500e+01,-2.5180e+01,-3.6590e+01,4.3280e+01,9.7100e+00,-2.0550e+01,1.3220e+01,3.3860e+01,-2.4020e+01,-1.8024e+02,1.1540e+01,1.3870e+01,8.6630e+01,5.9090e+01,-3.3160e+01,7.1200e+00,-4.8580e+01,9.8600e+00,-1.4310e+01,-7.0200e+00,1.1716e+02,-7.7800e+00,-5.9000e+00,4.6370e+01,4.0200e+00,7.3890e+01,5.7800e+00,-1.7190e+01,-1.8820e+01,1.3860e+01,-1.0199e+02,-9.6400e+00])
In [8]:1
change.shape
Out[8]:(242,)
In [9]:1
today = change[:-1]2
yestoday = change[1:]3
plt.scatter(today, yestoday)4
plt.show()5# 由结果可看出,相邻两天之间的差值并无关系
散点图的外观调整
In [10]:1
today = change[:-1]2
yestoday = change[1:]3# 参数含义依次是:横坐标;纵坐标;点的面积;点的颜色;点的形状;点的透明度4
plt.scatter(today, yestoday, s =100, c ='r', marker ='<', alpha =0.5)5
plt.show()
In []:1
3 折线图
1import numpy as np
2import matplotlib.pyplot as plt
3import matplotlib.dates as mdates
In [2]:1
x = np.linspace(-10,10,100)2
y = x **23
plt.plot(x, y)4
plt.show()
In [4]:1
date,open, close= np.loadtxt('000001.csv', delimiter =',', converters =2{0:mdates.bytespdate2num('%m/%d/%Y')}, skiprows =1, usecols =(0,1,4), unpack =True)3
date
A:\anaconda\anaconda\envs\tensorflow\lib\site-packages\ipykernel_launcher.py:1: MatplotlibDeprecationWarning:
The bytespdate2num classwas deprecated in Matplotlib 3.1and will be removed in3.3. Use time.strptime or dateutil.parser.parse or datestr2num instead."""Entry point for launching an IPython kernel.
A:\anaconda\anaconda\envs\tensorflow\lib\site-packages\ipykernel_launcher.py:1: MatplotlibDeprecationWarning:
The strpdate2num class was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use time.strptime or dateutil.parser.parse or datestr2num instead.
"""Entry point for launching an IPython kernel.
Out[4]:
array([735603.,735604.,735605.,735606.,735607.,735610.,735611.,735612.,735613.,735614.,735617.,735618.,735619.,735620.,735621.,735624.,735625.,735626.,735627.,735628.,735631.,735632.,735633.,735634.,735635.,735638.,735639.,735640.,735641.,735642.,735645.,735646.,735654.,735655.,735656.,735659.,735660.,735661.,735662.,735663.,735666.,735667.,735668.,735669.,735670.,735673.,735674.,735675.,735676.,735677.,735680.,735681.,735682.,735683.,735684.,735687.,735688.,735689.,735690.,735691.,735695.,735696.,735697.,735698.,735701.,735702.,735703.,735704.,735705.,735708.,735709.,735710.,735711.,735712.,735715.,735716.,735717.,735718.,735722.,735723.,735724.,735725.,735726.,735729.,735730.,735731.,735732.,735733.,735736.,735737.,735738.,735739.,735740.,735743.,735744.,735745.,735746.,735747.,735750.,735751.,735752.,735753.,735754.,735757.,735758.,735759.,735760.,735761.,735764.,735765.,735766.,735767.,735768.,735772.,735773.,735774.,735775.,735778.,735779.,735780.,735781.,735782.,735785.,735786.,735787.,735788.,735789.,735792.,735793.,735794.,735795.,735796.,735799.,735800.,735801.,735802.,735803.,735806.,735807.,735808.,735809.,735810.,735813.,735814.,735815.,735816.,735817.,735820.,735821.,735822.,735823.,735824.,735827.,735828.,735829.,735830.,735831.,735834.,735835.,735836.,735837.,735838.,735841.,735842.,735843.,735848.,735849.,735850.,735851.,735852.,735855.,735856.,735857.,735858.,735859.,735862.,735863.,735864.,735865.,735866.,735869.,735870.,735871.,735879.,735880.,735883.,735884.,735885.,735886.,735887.,735890.,735891.,735892.,735893.,735894.,735897.,735898.,735899.,735900.,735901.,735904.,735905.,735906.,735907.,735908.,735911.,735912.,735913.,735914.,735915.,735918.,735919.,735920.,735921.,735922.,735925.,735926.,735927.,735928.,735929.,735932.,735933.,735934.,735935.,735936.,735939.,735940.,735941.,735942.,735943.,735946.,735947.,735948.,735949.,735950.,735953.,735954.,735955.,735956.,735957.,735960.,735961.])
In [5]:1
plt.plot(date,open)2
plt.show()3# 会发现横坐标不是日期。因为python自动把日期转化成浮点数保存起来
In [6]:1
plt.plot_date(date,open)2
plt.show()3# 但是这是变成了散点图
In [7]:1
plt.plot_date(date,open,'-')2
plt.show()
In [8]:1# 参数:线型;颜色;点的形状2
plt.plot_date(date,open, linestyle ='-', color ='r', marker ='<')3
plt.show()
In [9]:1
plt.plot_date(date,open, linestyle ='-', color ='r', marker ='<')2
plt.plot_date(date, close, linestyle ='--', color ='b', marker ='o')3
plt.show()
In []:1
4 条形图
1import numpy as np
2import matplotlib.pyplot as plt
In [14]:1
index = np.arange(5)2
y1 =[10,25,20,30,35]3
pl = plt.bar(x = index, height = y)4
plt.show()
In [15]:1# 调整外观2
pl = plt.bar(x = index, height = y, color ='red', width =0.5)3
plt.show()
如何画水平方向的条形图
In [16]:1# 方法一2
pl = plt.bar(x =0, bottom = index, width = y, height =0.5, color ='red', orientation ='horizontal')3
plt.show()
In [19]:1# 方法二2
pl = plt.barh(left =0, y = index, width = y1, height =0.5, color ='red')3
plt.show()
并列式的条形图
In [24]:1
index = np.arange(4)2
sales_bj =[20,30,35,27]3
sales_sh =[19,29,32,35]4
bar_width =0.35
plt.bar(x = index, height = sales_bj, width = bar_width, color ='red')6
plt.bar(x = index + bar_width, height = sales_sh, width = bar_width, color ='blue')7
plt.show()
层叠式的条形图
In [25]:1
plt.bar(x = index, height = sales_bj, width = bar_width, color ='red')2
plt.bar(x = index, height = sales_sh, width = bar_width, bottom = sales_bj, color ='blue')3
plt.show()
In []:1
5 直方图
1import numpy as np
2import matplotlib.pyplot as plt
In [4]:1
mu =1002
sigma =203
x = mu + sigma * np.random.randn(2000)4# 参数含义:直方的个数;颜色;是否标准化(True表示用区间里的个数除以总个数,即频率)5
plt.hist(x, bins =10, color ='red', density =True)6
plt.show()
In [5]:1# 更改直方图外观2
plt.hist(x, bins =30, color ='green', density =False)3
plt.show()2-D直方图
In [7]:1
x = np.random.randn(2000)+22
y = np.random.randn(2000)+33
plt.hist2d(x, y, bins =50)4
plt.show()
In []:1
pylab方式作图
In [1]:1from pylab import*# 从pylab中导入了所有函数,所以可以直接使用
In [10]:1
x = arange(0,10)2
y = randn(len(x))3
title('pylab')4
plot(x, y)5
show()
pyplot作图
In [8]:1import matplotlib.pyplot as plt
2import numpy as np
In [11]:1
x = np.arange(0,10,1)2
y = np.random.randn(len(x))3
plt.plot(x, y)4
plt.title('pyplot')5
plt.show()
In []:1
8 子图-subplot
面向对象的方式创建子图
In [1]:1import matplotlib.pyplot as plt
2import numpy as np
In [7]:1
x = np.arange(0,100)2
fig = plt.figure()3
ax1 = fig.add_subplot(221)4
ax1.plot(x, x)5
ax2 = fig.add_subplot(222)6
ax2.plot(x,-x)7
ax3 = fig.add_subplot(223)8
ax3.plot(x, x*x)9
ax4 = fig.add_subplot(224)10
ax4.plot(x, np.log(x))
A:\anaconda\anaconda\envs\tensorflow\lib\site-packages\ipykernel_launcher.py:10: RuntimeWarning: divide by zero encountered in log
# Remove the CWD from sys.path while we load stuff.
Out[7]:[<matplotlib.lines.Line2D at 0x2d9f592b00>]
交互式方式创建子图
In [11]:1
plt.subplot(221)2
plt.plot(x, x)3
plt.subplot(222)4
plt.plot(x,-x)5
plt.subplot(223)6
plt.plot(x, x*x)7
plt.subplot(224)8
plt.plot(x, np.log(x))
A:\anaconda\anaconda\envs\tensorflow\lib\site-packages\ipykernel_launcher.py:8: RuntimeWarning: divide by zero encountered in log
Out[11]:[<matplotlib.lines.Line2D at 0x2da2752c18>]
In []:1
9 多图-figure
1import matplotlib.pyplot as plt
2import numpy as np
In [4]:1
fig1 = plt.figure()2
ax1 = fig1.add_subplot(111)3
ax1.plot([1,2,3],[3,2,1])4
fig2 = plt.figure()5
ax2 = fig2.add_subplot(111)6
ax2.plot([1,2,3],[1,2,3])7
plt.show()
In []:1
10 网格
两种方式绘制网格
1.用plt交互式方式绘制网格
In [1]:1import matplotlib.pyplot as plt
2import numpy as np
In [2]:1
y = np.arange(1,5)2
y
Out[2]:
array([1,2,3,4])
In [5]:1
plt.plot(y, y *2)2
plt.grid(True)
In [6]:1# 更改网格外观2
plt.plot(y, y *2)3
plt.grid(True, color ='red', linewidth =2, linestyle ='--')
面向对象方式创建网格
In [7]:1
x = np.arange(0,10,1)2
fig = plt.figure()3
ax = fig.add_subplot(111)4
plt.plot(x, x *2)5
ax.grid(color ='g')6
plt.show()
In []:1
11 图例
plt方式绘制图例
In [1]:1import matplotlib.pyplot as plt
2import numpy as np
In [2]:1
x = np.arange(1,11,1)2
x
Out[2]:
array([1,2,3,4,5,6,7,8,9,10])
In [4]:1
plt.plot(x, x *2, label ='Normal')2
plt.plot(x, x *3, label ='Fast')3
plt.plot(x, x *4, label ='Faster')4
plt.legend()5
plt.show()
图例的两个参数
1.位置参数
In [16]:1
plt.plot(x, x *2, label ='Normal')2
plt.plot(x, x *3, label ='Fast')3
plt.plot(x, x *4, label ='Faster')4
plt.legend(loc =0)# 参数由0到10。0表示自适应找到最佳位置5
plt.show()2.控制图例的形状
In [17]:1
plt.plot(x, x *2, label ='Normal')2
plt.plot(x, x *3, label ='Fast')3
plt.plot(x, x *4, label ='Faster')4
plt.legend(loc =0, ncol =3)# 3表示有3列5
plt.show()
也可以把label写进legend里面
In [18]:1
plt.plot(x, x *2)2
plt.plot(x, x *3)3
plt.plot(x, x *4)4
plt.legend(['Normal','Fast','Faster'], loc =0, ncol =3)5
plt.show()
用面向对象的方式绘制图例
In [21]:1
fig = plt.figure()2
ax = fig.add_subplot(111)3
l,= plt.plot(x, x)4# 第一种方式5
ax.legend(['ax legend'])6
plt.show()
In [22]:1
fig = plt.figure()2
ax = fig.add_subplot(111)3
l,= plt.plot(x, x)4# 第二种方式5
l.set_label('label via method')6
ax.legend()7
plt.show()
In [23]:1
fig = plt.figure()2
ax = fig.add_subplot(111)3# 第三种方式4
l,= plt.plot(x, x, label ='inline label')5
ax.legend()6
plt.show()
In []:1
12 坐标轴范围
1import matplotlib.pyplot as plt
2import numpy as np
In [2]:1
x = np.arange(-10,11,1)2
x
Out[2]:
array([-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10])
In [5]:1
plt.plot(x, x * x)2
plt.axis()# 显示坐标轴范围
Out[5]:(-11.0,11.0,-5.0,105.0)
In [6]:1
plt.plot(x, x * x)2
plt.axis([-5,5,20,60])# 指定坐标轴范围
Out[6]:[-5,5,20,60]
In [8]:1
plt.plot(x, x * x)2
plt.xlim()# 显示x轴的范围
Out[8]:(-11.0,11.0)
In [10]:1
plt.plot(x, x * x)2# 同plt.xlim(xmin = -5, xmax = 5)3
plt.xlim([-5,5])# 指定x轴的范围
Out[10]:(-5,5)
In [12]:1
plt.plot(x, x * x)2
plt.xlim([-5,5])3
plt.ylim([0,60])# 指定y轴的范围
Out[12]:(0,60)
In [13]:1
plt.plot(x, x * x)2
plt.xlim(xmin =-3)# 只指定一边
Out[13]:(-3,11.0)
In []:1
13 坐标轴刻度
1import matplotlib.pyplot as plt
2import numpy as np
In [4]:1
x = np.arange(1,11,1)2
ax = plt.plot(x, x)3
plt.show()
In [5]:1
x = np.arange(1,11,1)2
plt.plot(x, x)3
ax = plt.gca()# 获取当前图形的坐标轴4
ax.locator_params(nbins =20)# 表示坐标轴划分成多少个格子5
plt.show()
In [6]:1
x = np.arange(1,11,1)2
plt.plot(x, x)3
ax = plt.gca()# 获取当前图形的坐标轴4
ax.locator_params('x', nbins =20)# 只调整一个轴5
plt.show()
当坐标轴是日期时
In [7]:1import matplotlib.pyplot as plt
2import matplotlib as mpl
3import numpy as np
4import datetime
In [8]:1
fig = plt.figure()<Figure size 432x288 with0 Axes>
In [9]:1
start = datetime.datetime(2015,1,1)2
stop = datetime.datetime(2016,1,1)3
delta = datetime.timedelta(days =1)
In [10]:1
dates = mpl.dates.drange(start, stop, delta)2
y = np.random.rand(len(dates))
In [15]:1
ax = plt.gca()2
ax.plot_date(dates, y, linestyle ='-', marker ='')3
plt.show()
In []:1