日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python數(shù)據(jù)可視化matplotlib.pyplot

系統(tǒng) 1689 0

1.安裝matplotlib

          
            pip install matplotlib
          
        

2.繪制簡單圖形

          
            import matplotlib.pyplot as plt

#圖形輸入值
input_values = [1,2,3,4,5]
#圖形輸出值
squares = [1,4,9,16,25]

#plot根據(jù)列表繪制出有意義的圖形,linewidth是圖形線寬,可省略
plt.plot(input_values,squares,linewidth=5)
#設(shè)置圖標(biāo)標(biāo)題
plt.title("Square Numbers",fontsize = 24)
#設(shè)置坐標(biāo)軸標(biāo)簽
plt.xlabel("Value",fontsize = 14)
plt.ylabel("Square of Value",fontsize = 14)
#設(shè)置刻度標(biāo)記的大小
plt.tick_params(axis='both',labelsize = 14)
#打開matplotlib查看器,并顯示繪制圖形
plt.show()
          
        

3.繪制點

          
            import matplotlib.pyplot as plt

#繪制散點圖(傳如一對x和y坐標(biāo),在指定位置繪制一個點)
plt.scatter(2,4)
#設(shè)置輸出樣式
plt.scatter(3,5,s=200)
plt.show()
          
        

4.繪制一系列的點

          
            import matplotlib.pyplot as plt

x_values = [1,2,3,4,5]
y_values = [1,4,9,16,25]

plt.scatter(x_values,y_values,s=100)

plt.show()
          
        

5.自動計算數(shù)據(jù)

          
            import matplotlib.pyplot as plt

x_values = list(range(1,1001))
y_values = [x**2 for x in x_values]

plt.scatter(x_values,y_values,s=100)

#設(shè)置每個坐標(biāo)軸的取值范圍(x軸取值,y軸取值)
plt.axis([0,1100,0,1100000])
plt.show()
          
        

6.刪除數(shù)據(jù)點的輪廓

          
            import matplotlib.pyplot as plt

x_values = list(range(1,1001))
y_values = [x**2 for x in x_values]

#matplotlib允許你給散點圖中的各個點指定顏色。默認(rèn)為藍(lán)色點和黑色輪廓,在散點圖包含的 數(shù)據(jù)點不多時效果很好。但繪制很多點時,黑色輪廓可能會粘連在一起。
#edgecolor='none'刪除數(shù)據(jù)點的輪廓
plt.scatter(x_values,y_values,edgecolor='none', s=40)


#設(shè)置每個坐標(biāo)軸的取值范圍
plt.axis([0,1100,0,1100000])
plt.show()
          
        

7.自定義顏色c=''直接傳顏色或元組都可以

          
            import matplotlib.pyplot as plt

x_values = list(range(1,1001))
y_values = [x**2 for x in x_values]

#matplotlib允許你給散點圖中的各個點指定顏色。默認(rèn)為藍(lán)色點和黑色輪廓,在散點圖包含的 數(shù)據(jù)點不多時效果很好。但繪制很多點時,黑色輪廓可能會粘連在一起。
#edgecolor='none'刪除數(shù)據(jù)點的輪廓
plt.scatter(x_values, y_values,c='red', edgecolor='none', s=40)
# plt.scatter(x_values, y_values, c=(0, 0, 0.8), edgecolor='none', s=40)


#設(shè)置每個坐標(biāo)軸的取值范圍
plt.axis([0,1100,0,1100000])
plt.show()
          
        

8.使用顏色映射

          
            import matplotlib.pyplot as plt

x_values = list(range(1,1001))
y_values = [x**2 for x in x_values]

#matplotlib允許你給散點圖中的各個點指定顏色。默認(rèn)為藍(lán)色點和黑色輪廓,在散點圖包含的 數(shù)據(jù)點不多時效果很好。但繪制很多點時,黑色輪廓可能會粘連在一起。
#edgecolor='none'刪除數(shù)據(jù)點的輪廓
plt.scatter(x_values, y_values,c=y_values,cmap=plt.cm.Blues, edgecolor='none', s=40)


#設(shè)置每個坐標(biāo)軸的取值范圍
plt.axis([0,1100,0,1100000])
plt.show()
          
        

9.自動保存圖表

          
            import matplotlib.pyplot as plt

x_values = list(range(1,1001))
y_values = [x**2 for x in x_values]

#matplotlib允許你給散點圖中的各個點指定顏色。默認(rèn)為藍(lán)色點和黑色輪廓,在散點圖包含的 數(shù)據(jù)點不多時效果很好。但繪制很多點時,黑色輪廓可能會粘連在一起。
#edgecolor='none'刪除數(shù)據(jù)點的輪廓
plt.scatter(x_values, y_values,c=y_values,cmap=plt.cm.Blues, edgecolor='none', s=40)


#設(shè)置每個坐標(biāo)軸的取值范圍
plt.axis([0,1100,0,1100000])
# plt.show()
#參數(shù)1指定要以什么樣的文件名保存圖表,保存和代碼的同目錄下,第二個參數(shù)表示要將多余的空白區(qū)域剪掉,要保留空白區(qū)域,可省略第二個參數(shù)
plt.savefig('squares_plot.png',bbox_inches='tight')
          
        

10.隨機(jī)漫步(繪制隨機(jī)漫步圖)

          
            from random import choice

class RandomWalk(object):
    """一個生成隨機(jī)漫步數(shù)據(jù)的類"""
    def __init__(self, num_points = 5000):
        """初始化隨機(jī)漫步的屬性"""
        #存儲隨機(jī)漫步次數(shù)的變量
        self.num_points = num_points
        #所有隨機(jī)漫步都始于(0,0)
        #分別存儲隨機(jī)漫步經(jīng)過的每個點的x和y坐標(biāo)
        self.x_values = [0]
        self.y_values = [0]
        

    def fill_walk(self):
        """計算隨機(jī)漫步包含的所有點"""

        #不斷漫步,直到列表達(dá)到指定的長度
        while len(self.x_values) < self.num_points:
            #決定前進(jìn)方向以及沿這個方向前進(jìn)的距離
            x_direction = choice([1,-1])
            x_distance = choice([0,1,2,3,4])
            x_step = x_direction * x_distance
            
            y_direction = choice([1,-1])
            y_distance = choice([0,1,2,3,4])
            y_step = y_direction * y_distance

            #拒絕原地踏步
            if x_step == 0 and y_step == 0:
                continue

            #計算下一個點的x值和y值
            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)
        pass
          
        

繪制隨機(jī)漫步圖

          
            import matplotlib.pyplot as plt

from random_walk import RandomWalk


#創(chuàng)建一個RandomWalk實例,并將其包含的點都繪制出來

rw = RandomWalk()

rw.fill_walk()

plt.scatter(rw.x_values,rw.y_values,s=15)
plt.show()
          
        

11.模擬多次隨機(jī)漫步

          
            import matplotlib.pyplot as plt

from random_walk import RandomWalk


#只要程序處于活動狀態(tài),就不斷的模擬漫步

while True:
    #創(chuàng)建一個RandomWalk實例,并將其包含的點都繪制出來
    rw = RandomWalk()
    rw.fill_walk()

    plt.scatter(rw.x_values,rw.y_values,s=15)
    plt.show()
    
    keep_running = input("Make another walk?(y/n)")
    if keep_running=='n':
        break
          
        

12.給點著色

          
            import matplotlib.pyplot as plt

from random_walk import RandomWalk


#只要程序處于活動狀態(tài),就不斷的模擬漫步

while True:
    #創(chuàng)建一個RandomWalk實例,并將其包含的點都繪制出來
    rw = RandomWalk()
    rw.fill_walk()

    point_numbers = list(range(rw.num_points))

    plt.scatter(rw.x_values,rw.y_values,c=point_numbers,cmap=plt.cm.Blues,edgecolor='none',s=15)
    plt.show()
    
    keep_running = input("Make another walk?(y/n)")
    if keep_running=='n':
        break

          
        

13.重新繪制起點和終點

          
            import matplotlib.pyplot as plt

from random_walk import RandomWalk


#只要程序處于活動狀態(tài),就不斷的模擬漫步

while True:
    #創(chuàng)建一個RandomWalk實例,并將其包含的點都繪制出來
    rw = RandomWalk()
    rw.fill_walk()

    point_numbers = list(range(rw.num_points))

    plt.scatter(rw.x_values,rw.y_values,c=point_numbers,cmap=plt.cm.Blues,edgecolors='none',s=15)
    
    #突出起點和終點
    plt.scatter(0,0,c='green',edgecolor='none',s=100)
    plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolors='none',s=100)

    plt.show()

    keep_running = input("Make another walk?(y/n)")
    if keep_running=='n':
        break

          
        

14.隱藏坐標(biāo)軸

          
            while True:
    #創(chuàng)建一個RandomWalk實例,并將其包含的點都繪制出來
    rw = RandomWalk()
    rw.fill_walk()

    point_numbers = list(range(rw.num_points))

    plt.scatter(rw.x_values,rw.y_values,c=point_numbers,cmap=plt.cm.Blues,edgecolors='none',s=15)
    
    #突出起點和終點
    plt.scatter(0,0,c='green',edgecolor='none',s=100)
    plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolors='none',s=100)

    #隱藏坐標(biāo)軸
    plt.axes().get_xaxis().set_visible(False)
    plt.axes().get_yaxis().set_visible(False)

    plt.show()

    keep_running = input("Make another walk?(y/n)")
    if keep_running=='n':
        break
          
        

15.增加點數(shù)(增加點數(shù),將每個點的大小調(diào)小)

          
            while True:
    #創(chuàng)建一個RandomWalk實例,并將其包含的點都繪制出來
    rw = RandomWalk(50000)
    rw.fill_walk()

    point_numbers = list(range(rw.num_points))

    plt.scatter(rw.x_values,rw.y_values,c=point_numbers,cmap=plt.cm.Blues,edgecolors='none',s=1)
    
    #突出起點和終點
    plt.scatter(0,0,c='green',edgecolor='none',s=100)
    plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolors='none',s=100)

    #隱藏坐標(biāo)軸
    plt.axes().get_xaxis().set_visible(False)
    plt.axes().get_yaxis().set_visible(False)

    plt.show()

    keep_running = input("Make another walk?(y/n)")
    if keep_running=='n':
        break
          
        

17.調(diào)整尺寸以適應(yīng)屏幕

          
            while True:
    #創(chuàng)建一個RandomWalk實例,并將其包含的點都繪制出來
    rw = RandomWalk(50000)
    rw.fill_walk()

    #設(shè)置繪圖窗口的尺寸
    #figure()用于指定圖表的寬度,高度,分辨率黑背景色figsize需要指定一個元組,單位英寸,dpi是分辨率,可傳可不傳
    plt.figure(dpi=128,figsize=(10,6))

    point_numbers = list(range(rw.num_points))

    plt.scatter(rw.x_values,rw.y_values,c=point_numbers,cmap=plt.cm.Blues,edgecolors='none',s=1)
    
    #突出起點和終點
    plt.scatter(0,0,c='green',edgecolor='none',s=100)
    plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolors='none',s=100)

    #隱藏坐標(biāo)軸
    plt.axes().get_xaxis().set_visible(False)
    plt.axes().get_yaxis().set_visible(False)

    plt.show()

    keep_running = input("Make another walk?(y/n)")
    if keep_running=='n':
        break
          
        

更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 衡东县| 石林| 晴隆县| 那曲县| 光山县| 吴川市| 克拉玛依市| 合江县| 九龙城区| 安国市| 杂多县| 孙吴县| 贵溪市| 平果县| 通许县| 晋宁县| 乐陵市| 宁河县| 白沙| 商河县| 酉阳| 赣榆县| 留坝县| 成都市| 东丰县| 吴桥县| 顺义区| 娄烦县| 嘉荫县| 渝北区| 萝北县| 西丰县| 城步| 灌阳县| 大田县| 青铜峡市| 潼关县| 双辽市| 孟州市| 吉木萨尔县| 太保市|