數(shù)據(jù)可視化指的是通過(guò)可視化表示探索數(shù)據(jù),它與數(shù)據(jù)挖掘緊密相關(guān)髓棋,而數(shù)據(jù)挖掘指的是用代碼來(lái)探索數(shù)據(jù)集的規(guī)律和關(guān)聯(lián)喧锦。
數(shù)據(jù)集可以是用一小行代碼表示的小型數(shù)字列表,也可以是達(dá)到幾百吉字節(jié)的數(shù)據(jù)丁频。
1>利用matplotlib繪制各種圖表杉允。文檔
示例一:
1、通過(guò)plot()函數(shù)繪制立方函數(shù)圖的5個(gè)立方數(shù)的折線圖:
2席里、plt.title()設(shè)置表的標(biāo)題以及size設(shè)置對(duì)應(yīng)的字體大小
3叔磷、plt.xlabel和plt.ylabel設(shè)置x軸和y軸的標(biāo)識(shí)以及字體的大小
4、plt.tick_params()里面的參數(shù)設(shè)置兩個(gè)坐標(biāo)軸的刻度的大小
5奖磁、8幕!署穗!plt.show()把圖標(biāo)展示出來(lái),如果想存儲(chǔ)圖片一定要在show前調(diào)用plt.savefig("chart.jpg")
效果圖:
import matplotlib.pyplot as plt
#draw the chart of five cubic number
x_values=list(range(1,6))
y_values=[x**3 for x in x_values]
plt.plot(x_values,y_values,linewidth=5,c=(0,0,1))
plt.title("Cubic number",size=24)
plt.xlabel("Value",size=14)
plt.ylabel("Result",size=14)
plt.tick_params(axis="both",labelsize=14)
#to save the chart as jpg
plt.savefig("cubic chart",bbox_inches='tight')
plt.show()
示例二:
繪制一個(gè)含5000個(gè)點(diǎn)的立方彩圖:
2>利用plt.scatter()繪制一系列點(diǎn)
1寥裂、Scatter()里面的cmap是一個(gè)顏色顏色映射,并且通過(guò)c=y_value指定了顏色的深淺隨著y值越大案疲,
藍(lán)色表現(xiàn)得越深封恰。
效果圖:
import matplotlib.pyplot as plt
#draw the chart of five cubic number
x_values=list(range(1,50001))
y_values=[x**3 for x in x_values]
plt.scatter(x_values,y_values,c=y_values,cmap=plt.cm.Blues,edgecolor='none',s=40)
plt.title("Cubic number",size=24)
plt.xlabel("Value",size=14)
plt.ylabel("Result",size=14)
plt.tick_params(axis="both",labelsize=14)
#to save the chart as jpg
plt.savefig("cubic chart",bbox_inches='tight')
plt.show()
示例三:(綜合)
畫出來(lái)的圖描述分子運(yùn)動(dòng),先寫一個(gè)RandomWalk類褐啡,分子的移動(dòng)距離通過(guò)函數(shù)get_step獲得诺舔,分子的移動(dòng)則通過(guò)函數(shù)fill_walk()實(shí)現(xiàn),就是把原位置加上step后的新位置存儲(chǔ)在列表里。最后通過(guò)列表的點(diǎn)把畫出來(lái)實(shí)現(xiàn)低飒。
import matplotlib.pyplot as plt
import random
class Randomwalk(object):
def __init__(self,num_points=5000):
self.num_points=num_points
#all data are sart at (0,0)
self.x_values=[0]
self.y_values=[0]
def get_step(self):
direction=random.choice([-2,-1,0,1,2])
distance=random.choice([0,1,2,3,4,5,6,7,8,9])
step=direction*distance
#print step
return step
def fill_walk(self,x_step,y_step):
#decide to move the distance and the direction of the movement
#refuse the step=0
#computing the next point of the distance and direction
next_x=self.x_values[-1]+x_step
next_y=self.y_values[-1]+y_step
self.x_values.append(next_x)
self.y_values.append(next_y)
#instablished a Randomwalk object,and draw all the points it included
while True:
rw=Randomwalk(80000)
while len(rw.x_values)<rw.num_points:
x_step=rw.get_step()
y_step=rw.get_step()
rw.fill_walk(x_step, y_step)
point_numbers=list(range(rw.num_points))
#改變屏幕的尺寸
plt.figure(figsize=(10,6))
#把坐標(biāo)為(0许昨,0)的點(diǎn)的顏色設(shè)置為綠色,邊緣顏色設(shè)置為none褥赊。size(大小)設(shè)置為100
plt.scatter(0,0,c='green',edgecolor='none',s=100)
plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolor='none',s=100)
#把折線的粗細(xì)設(shè)置為2糕档,顏色設(shè)置為紅色
plt.plot(rw.x_values,rw.y_values,linewidth=2,c='red')
#把坐標(biāo)軸隱藏
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
plt.show()
answer=raw_input()
if answer=='n':
break```