數(shù)據(jù)可視化之利用matplotlib畫離散點圖和折現(xiàn)圖

數(shù)據(jù)可視化指的是通過可視化表示探索數(shù)據(jù),它與數(shù)據(jù)挖掘緊密相關(guān),而數(shù)據(jù)挖掘指的是用代碼來探索數(shù)據(jù)集的規(guī)律和關(guān)聯(lián)。
數(shù)據(jù)集可以是用一小行代碼表示的小型數(shù)字列表,也可以是達到幾百吉字節(jié)的數(shù)據(jù)。

1>利用matplotlib繪制各種圖表。文檔
示例一:
1、通過plot()函數(shù)繪制立方函數(shù)圖的5個立方數(shù)的折線圖:
2、plt.title()設(shè)置表的標題以及size設(shè)置對應(yīng)的字體大小
3、plt.xlabel和plt.ylabel設(shè)置x軸和y軸的標識以及字體的大小
4、plt.tick_params()里面的參數(shù)設(shè)置兩個坐標軸的刻度的大小
5、?。?!plt.show()把圖標展示出來,如果想存儲圖片一定要在show前調(diào)用plt.savefig("chart.jpg")

效果圖:


圖片.png
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()

示例二:
繪制一個含5000個點的立方彩圖:
2>利用plt.scatter()繪制一系列點
1、Scatter()里面的cmap是一個顏色顏色映射,并且通過c=y_value指定了顏色的深淺隨著y值越大,
藍色表現(xiàn)得越深。
效果圖:

圖片.png
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()

示例三:(綜合)

效果圖

畫出來的圖描述分子運動,先寫一個RandomWalk類,分子的移動距離通過函數(shù)get_step獲得,分子的移動則通過函數(shù)fill_walk()實現(xiàn),就是把原位置加上step后的新位置存儲在列表里。最后通過列表的點把畫出來實現(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))

    #把坐標為(0,0)的點的顏色設(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)
    #把折線的粗細設(shè)置為2,顏色設(shè)置為紅色
    plt.plot(rw.x_values,rw.y_values,linewidth=2,c='red')
#把坐標軸隱藏
    plt.axes().get_xaxis().set_visible(False)
    plt.axes().get_yaxis().set_visible(False)
    plt.show()
    answer=raw_input()
    if answer=='n':
        break```   
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容