Python——Numpy、Pandas、Matplotlib

Python——Numpy、Pandas、Matplotlib

Matplotlib<day01>

 今日思惟導圖整理

今日思惟導圖整理dom

繪製圖以下

  • 折線圖

  • # -*- coding:utf-8 -*-  
    # ====#====#====#====#====#====#====#====#====
    # @Time    : 2020/4/4 11:30
    # @Author  : Alex_Dong
    # @Email   : 1220274707@qq.com
    # @HomePage:https://www.cnblogs.com/xied/  
    # @File    : 演示文件.py
    # @Software: PyCharm
    # ====#====#====#====#====#====#====#====#====
    
    
    # import matplotlib
    
    # 繪製一條直線
    import matplotlib.pyplot as plt
    
    x = range(2, 27, 2)
    y = [15, 13, 14, 5, 17, 20, 25, 26, 27, 25, 22, 18, 15]
    
    # plt.plot(x,y)
    # plt.show()
    # plt.title('這是折線圖的標題')
    
    # 設置大小,高寬及像素dpi
    # fig=plt.figure(figsize=(20,8),dpi=100)
    # figsize接收一個元組,表示圖片的高和寬,單位是英寸
    # DPI分辨率,表明了每一英寸有多少像素,默認80
    # plt.plot(x,y)
    # plt.show()
    
    # 保存圖片
    # fig.savefig('./pic/test_png.png')
    # 能夠保存爲SVG這種矢量圖格式,放大之後不會有鋸齒
    # fig.savefig("./pic/test_svg.svg")
    
    # X軸和Y軸的調整和設置中文
    # plt.plot(x,y)
    # x軸的刻(度)
    # plt.xticks(x)
    # plt.yticks(y)
    # plt.show()
    
    # import random
    # import matplotlib as mpl
    # # 設置中文格式'仿宋'
    # mpl.rcParams['font.sans-serif'] = ['FangSong']
    # mpl.rcParams['font.size'] = 16
    #
    # y = [random.randint(15, 35) for i in range(10)]
    # x = list(range(10))
    # fig = plt.figure(figsize=(20, 8))
    # plt.plot(x, y)
    # xlable = ['10點{}分'.format(i) for i in range(60)]
    # xlable += ['11點{}分'.format(i) for i in range(60)]
    #
    # #添加描述
    # plt.xlabel('時間',color='red',fontdict={'fontsize':20})
    # plt.ylabel('溫度')
    # #設置標題
    # plt.title('某日10點到12點間的溫度變化狀況')
    # #添加網格
    # plt.grid(alpha=0.1)
    #
    # plt.xticks(x[::1], xlable[::12], rotation=45)
    # plt.yticks(y)
    # plt.show()
    # fig.savefig('./pic/fansong_text_png.png')
    
    
    import numpy as np
    import matplotlib.pyplot as plt
    
    plt.figure(1)  # 建立圖表1
    plt.figure(2)  # 建立圖表2
    ax1 = plt.subplot(211)  # 在圖表2中建立子圖1
    ax2 = plt.subplot(212)  # 在圖表2中建立子圖2
    
    x = np.linspace(0, 3, 100)
    for i in range(5):
        plt.figure(1)  # ❶ # 選擇圖表1
        plt.plot(x, np.exp(i * x / 3))
        plt.sca(ax1)  # ❷ # 選擇圖表2的子圖1
        plt.plot(x, np.sin(i * x))
        plt.sca(ax2)  # 選擇圖表2的子圖2
        plt.plot(x, np.cos(i * x))
    
    plt.show()

    View Code

     

  • 散點圖

  • # -*- coding:utf-8 -*-  
    # ====#====#====#====#====#====#====#====#====
    # @Time    : 2020/4/4 14:05
    # @Author  : Alex_Dong
    # @Email   : 1220274707@qq.com
    # @HomePage:https://www.cnblogs.com/xied/  
    # @File    : 散點圖.py
    # @Software: PyCharm
    # ====#====#====#====#====#====#====#====#====
    
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    
    # 設置中文說明'仿宋'字體
    
    mpl.rcParams['font.sans-serif'] = ['FangSong'] # 用來正常顯示中文標籤
    mpl.rcParams['font.size'] = 16
    
    x_3 = list(range(1, 32))
    y_3 = [10, 16, 17, 14, 12, 10, 12, 6, 6, 7, 8, 9, 12, 15, 15, 17, 18, 21, 16, 16, 20, 13, 15, 15, 15, 18, 20, 22, 22,
           22, 24]
    x_10 = [i + 50 for i in x_3]
    y_10 = [26, 26, 28, 19, 21, 17, 16, 19, 18, 20, 20, 19, 22, 23, 17, 20, 21, 20, 22, 15, 11, 15, 5, 13, 17, 10, 11, 13,
            12, 13, 6]
    
    fig = plt.figure(figsize=(15, 8))
    
    plt.scatter(x_3, y_3, label='三月份')
    plt.scatter(x_10, y_10, label='十月份')
    
    # 設置軸刻度
    # 集合
    y = set(y_3 + y_10)
    min_y = min(y)
    max_y = max(y)
    plt.yticks(range(min_y, max_y + 1))
    # x軸
    
    x = x_3 + x_10
    x_lables = ['3月{}日'.format(i) for i in range(1, 32)] + ['10月{}日'.format(i) for i in range(1, 32)]
    plt.xticks(x[::2], x_lables[::2], rotation=45)
    plt.xlabel('日期')
    plt.ylabel('溫度(C)')
    plt.title('武漢市2019年3月份到10月份的氣溫變化趨勢圖')
    #添加網格
    plt.grid(alpha=0.3)
    # 設置圖例
    plt.legend()
    
    plt.show()
    fig.savefig('./pic/散點圖.png')
    View Code

     

  • 條形圖

  • # -*- coding:utf-8 -*-  
    # ====#====#====#====#====#====#====#====#====
    # @Time    : 2020/4/4 19:52
    # @Author  : Alex_Dong
    # @Email   : 1220274707@qq.com
    # @HomePage:https://www.cnblogs.com/xied/  
    # @File    : 條形圖.py
    # @Software: PyCharm
    # ====#====#====#====#====#====#====#====#====
    
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    
    # 設置中文
    mpl.rcParams['font.sans-serif'] = ['FangSong']  # 用來正常顯示中文標籤
    mpl.rcParams['font.size'] = 16  # 設置字體大小
    
    # 構建座標
    movies = x = ['哪吒之魔童降世', '流浪地球', '復仇者聯盟4:終局之戰', '瘋狂的外星人', '飛馳人生', '烈火英雄', '速度與激情:特別行動', '蜘蛛俠:英雄遠征', '掃毒2天地對決', '大黃蜂',
                  '驚奇隊長', '比悲傷更悲傷的故事', '哥斯拉2:怪獸之王', '阿麗塔:戰鬥天使', '銀河補習班', '獅子王', '反貪風暴4 ', '熊出沒·原始時代', '使徒行者2:諜影行動',
                  '大偵探皮卡丘']
    y = [49.04, 46.18, 42.05, 21.83, 17.03, 16.74, 14.16, 14.01, 12.85, 11.38, 10.25, 9.46, 9.27, 8.88, 8.64, 8.23, 7.88,
         7.09, 6.92, 6.34]
    x = range(len(movies))
    # 設置容器
    fig = plt.figure(figsize=(20, 8), dpi=100)
    # 繪圖
    plt.barh(x, y, )
    # 設置軸刻度
    plt.xticks(x, rotation=90)
    plt.xlabel('電影名稱')
    plt.ylabel('電影票房')
    plt.title('2019年電影的票房')
    # 設置網格
    plt.grid(alpha=0.3)
    # 設置圖例
    # plt.legend()
    # 顯示
    plt.show()
    # 儲存
    fig.savefig('./pic/條形圖.png')
    View Code

     

  • 多合一

  • # -*- coding:utf-8 -*-  
    # ====#====#====#====#====#====#====#====#====
    # @Time    : 2020/4/4 20:47
    # @Author  : Alex_Dong
    # @Email   : 1220274707@qq.com
    # @HomePage:https://www.cnblogs.com/xied/  
    # @File    : 繪製多圖表.py
    # @Software: PyCharm
    # ====#====#====#====#====#====#====#====#====
    
    import numpy as np
    import matplotlib.pyplot as plt
    
    fig = plt.figure(2,figsize=(20, 8), dpi=100)
    # fig = plt.figure(5,figsize=(20, 8), dpi=100)
    
    # fig_1 = plt.figure(1)  # 建立圖表1
    # fig_2 = plt.figure(2)  # 建立圖表2
    # ax1 = plt.subplot(211)  # 在圖表2中建立子圖1
    # ax2 = plt.subplot(212)  # 在圖表2中建立子圖2
    ax1 = fig.add_subplot(211)
    ax2 = fig.add_subplot(212)
    
    
    x = np.linspace(0, 3, 100)
    for i in range(5):
        plt.figure(1)  # ❶ # 選擇圖表1
        plt.plot(x, np.exp(i * x / 3))
        plt.sca(ax1)  # ❷ # 選擇圖表2的子圖1
        plt.plot(x, np.sin(i * x))
        plt.sca(ax2)  # 選擇圖表2的子圖2
        plt.plot(x, np.cos(i * x))
    
    # plt.show()
    fig.show()
    # fig.savefig('./pic/繪製多圖表.png')
    # fig.savefig("./pic/test.pdf")
    View Code
    # -*- coding:utf-8 -*-  
    # ====#====#====#====#====#====#====#====#====
    # @Time    : 2020/4/4 13:59
    # @Author  : Alex_Dong
    # @Email   : 1220274707@qq.com
    # @HomePage:https://www.cnblogs.com/xied/  
    # @File    : 一個圖中畫多個圖.py
    # @Software: PyCharm
    # ====#====#====#====#====#====#====#====#====
    
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    
    # 設置中文
    mpl.rcParams['font.sans-serif'] = ['FangSong']  # 用來正常顯示中文標籤
    mpl.rcParams['font.size'] = 16  # 設置字體大小
    # 構建座標
    # x軸表示年齡,Y軸表示個數
    x = range(11, 31)
    y_self = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
    y_d = [1, 0, 3, 1, 2, 2, 3, 3, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]
    # 建立容器
    fig = plt.figure(figsize=(20, 8))
    # 畫圖
    plt.plot(x, y_self, label='本身', color='black', linestyle='-.')
    plt.plot(x, y_d, label='同桌')
    # 設置刻度
    x_lables = ['{}歲'.format(i) for i in x]
    plt.xticks(x, x_lables)
    # 設置標籤
    plt.xlabel('年齡')
    plt.ylabel('個數')
    plt.title('我和同桌曆年的個數對比')
    # 設置了圖例必定要加上這句話
    plt.legend()
    plt.grid(alpha=0.3)
    # 標記點
    plt.annotate('最高點', xy=(23, 6), xytext=(24, 6), arrowprops={'arrowstyle': '<->'})
    plt.show()
    fig.savefig('./pic/一張圖畫多個.png')
    View Code
    # -*- coding:utf-8 -*-  
    # ====#====#====#====#====#====#====#====#====
    # @Time    : 2020/4/4 21:29
    # @Author  : Alex_Dong
    # @Email   : 1220274707@qq.com
    # @HomePage:https://www.cnblogs.com/xied/  
    # @File    : pylib.py
    # @Software: PyCharm
    # ====#====#====#====#====#====#====#====#====
    
    import numpy as np
    import pylab as pl
    
    x = [1, 2, 3, 4, 5]  # Make an array of x values
    y = [1, 4, 9, 16, 25]  # Make an array of y values for each x value
    
    # pl.plot(x, y)  # use pylab to plot x and y  直接畫是線段
    # pl.plot(x, y, 'o')   #變成散點
    pl.plot(x, y, 'or')    #散點變爲紅色
    pl.show()  # show the plot on the screen
    View Code

     

相關文章
相關標籤/搜索