python常用可視化包matplotlib入門教程

常見用法

在這裏插入圖片描述

代碼演示

在這裏插入圖片描述


# from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
from random import choice
# 類的簡單使用
class dog():
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def sit(self):
        print(self.name.title() + " is now sitting.")

    def roll_over(self):
        print(self.name.title() + " rolled over!")

# matplotlib的簡單使用
class matPlot():
    def __init__(self):
        print("簡單的繪圖練習")

    # 簡易的使用
    def test_1(self):
        y = [1, 2, 5, 8, 9]
        plt.plot(y)
        plt.show()

    # 設置x , y , title
    def test_2(self):
        y = [1, 2, 5, 8, 9]
        plt.plot(y, linewidth=5)
        # 標題
        plt.title("title", fontsize=20)
        # x軸
        plt.xlabel("x", fontsize=15)
        # y軸
        plt.ylabel("y", fontsize=15)
        # 刻度尺樣式,字體大小爲15
        plt.tick_params(axis='both',  labelsize=15)
        plt.show()

    # 同時設置x和y對應關係
    def test_3(self):
        x = [1,2,3,4,5]
        y = [2,4,6,8,0]
        plt.plot(x,y)
        plt.show()

    # 散點圖
    def test_4(self):
        x = [1,2,3,4,5]
        y = [2,4,6,8,0]
        plt.scatter(x,y)
        plt.show()     
    
    # 自動計算
    def test_5(self):
        x_value = list(range(1, 1001))
        y_value = [x**2 for x in x_value]
        plt.scatter(x_value,y_value)
        # 設置x,y軸的刻度,x[0,1001] y[1,100001]
        plt.axis([0,1001,1,100001])
        plt.show()

    # 設置x,y軸的刻度
    def test_6(self):
        x_value = list(range(1, 1001))
        y_value = [x**2 for x in x_value]
        # 刪除數據的輪廓
        plt.scatter(x_value,y_value, edgecolors=None, s=9)
        # 設置x,y軸的刻度,x[0,1001] y[1,100001]
        plt.axis([0,1001,1,100001])
        plt.show()

    # 設置線條的顏色 默認藍色
    def test_7(self):
        x_value = list(range(1, 1001))
        y_value = [x**2 for x in x_value]
        # 設置顏色
        plt.scatter(x_value,y_value, edgecolors=None, s=9, c='red')
        plt.axis([0,1001,1,100001])
        plt.show()

    # 使用顏色漸變 參數 cmap 告訴 pyplot 使用哪個顏色映射
    def test_8(self):
        x_value = list(range(1, 1001))
        y_value = [x**2 for x in x_value]
        # 設置顏色
        plt.scatter(x_value,y_value, edgecolors=None, s=9, c= y_value, cmap=plt.cm.Blues)
        plt.axis([0,1001,1,100001])
        plt.show()

    # 自動保存圖
    def test_9(self):
        x_value = list(range(1, 1001))
        y_value = [x**2 for x in x_value]
        # 設置顏色
        plt.scatter(x_value,y_value, edgecolors=None, s=9, c= y_value, cmap=plt.cm.Blues)
        plt.axis([0,1001,1,100001])
        # 第二個實參指定將圖表多餘的空白區域裁剪掉。如果要保留圖表周圍多餘的空白區域,可省略這個實參
        plt.savefig("xxx.png", bbox_inches='tight')

    def test_10(self):
        # x,y 對應關係
        x_value = list(range(1, 1001))
        y_value = [x**2 for x in x_value]
        # 設置藍色漸變
        plt.scatter(x_value,y_value, edgecolors=None, s=9, c= y_value, cmap=plt.cm.Blues)
        # 設置x ,y軸的標題
        plt.xlabel("xlabel", fontsize=13)
        plt.ylabel("ylabel",fontsize=13)
        # 設置x,y軸刻度尺
        plt.axis([0,1001,1,100001])
        # 設置標題
        plt.title("done with this page")
        # 第二個實參指定將圖表多餘的空白區域裁剪掉。如果要保留圖表周圍多餘的空白區域,可省略這個實參
        plt.savefig("xxx.png", bbox_inches='tight')

class randomWalk():
    def __init__(self, number_point = 5000):
        print("隨機漫步練習")
        # 初始化屬性
        self.number_point = number_point
        # 設置起點
        self.x_values = [0]
        self.y_values = [0]
    
    def fill_walk(self):
        while len(self.x_values) < self.number_point:
            # 前進方向和前進距離
            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)
        print("done")
   

if __name__ == "__main__":
    # 類的練習
    dog1 = dog("dog_a", 12)
    # dog1.sit()
    # dog1.roll_over()

    # 開啓matplotlib的入門練習之旅
    matplotlibTest = matPlot()
    # matplotlibTest.test_1()
    # matplotlibTest.test_2()
    # matplotlibTest.test_3()
    # matplotlibTest.test_4()
    # matplotlibTest.test_5()
    # matplotlibTest.test_6()
    # matplotlibTest.test_7()
    # matplotlibTest.test_8()
    # matplotlibTest.test_9()
    # matplotlibTest.test_10()


    random_walk_test = randomWalk()
    random_walk_test.fill_walk()
    plt.scatter(random_walk_test.x_values, random_walk_test.y_values, s=15)
    plt.title("random walk")
    plt.show()


常見報錯

python導入包失敗ModuleNotFoundError: No module named ‘matplotlib.pyplot’; ‘matplotlib’ is not a package

文件名稱命名問題,不要將文件名命名爲matplotlib.py

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章