日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

数据可视化组队学习:《Task03 - 布局格式定方圆》笔记

發(fā)布時間:2025/3/15 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据可视化组队学习:《Task03 - 布局格式定方圆》笔记 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 前言
  • 1 子圖
    • 1.1 使用 plt.subplots 繪制均勻狀態(tài)下的子圖
    • 1.2 使用 GridSpec 繪制非均勻子圖
  • 2 子圖上的方法
    • 2.1 plot-線的繪制
    • 2.2 hist-直方圖的繪制
    • 2.3 對圖像的特殊設(shè)置
  • 作業(yè)

前言

《第三回:布局格式定方圓》筆記。


1 子圖

1.1 使用 plt.subplots 繪制均勻狀態(tài)下的子圖

""" figsize 參數(shù)可以指定整個畫布的大小sharex 和 sharey 分別表示是否共享橫軸和縱軸刻度tight_layout 函數(shù)可以調(diào)整子圖的相對大小使字符不會重疊 """fig, axs = plt.subplots(2, 5, figsize=(10, 4), sharex=True, sharey=True) fig.suptitle('樣例1', size=20) for i in range(2):for j in range(5):axs[i][j].scatter(np.random.randn(10), np.random.randn(10))axs[i][j].set_title('第%d行,第%d列'%(i+1,j+1))axs[i][j].set_xlim(-5,5)axs[i][j].set_ylim(-5,5)if i==1: axs[i][j].set_xlabel('橫坐標(biāo)')if j==0: axs[i][j].set_ylabel('縱坐標(biāo)') fig.tight_layout()

使用plt.subplot()和prejection船艦極坐標(biāo)系下的圖表:

N = 150 r = 2 * np.random.rand(N) theta = 2 * np.pi * np.random.rand(N) area = 200 * r**2 colors = thetaplt.subplot(projection='polar') plt.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)

1.2 使用 GridSpec 繪制非均勻子圖

非均勻包含兩層含義:

  • 圖的比例大小不同但沒有跨行或跨列
  • 圖為跨列或跨行狀態(tài)
  • 第一種含義:

    fig = plt.figure(figsize=(10, 4)) spec = fig.add_gridspec(nrows=2, ncols=5, width_ratios=[1,2,3,4,5], height_ratios=[1,3]) fig.suptitle('樣例2', size=20) for i in range(2):for j in range(5):ax = fig.add_subplot(spec[i, j]) # gridspec子圖。可以通過切片獲取ax.scatter(np.random.randn(10), np.random.randn(10))ax.set_title('第%d行,第%d列'%(i+1,j+1))if i==1: ax.set_xlabel('橫坐標(biāo)')if j==0: ax.set_ylabel('縱坐標(biāo)') fig.tight_layout()

    第二種:
    可以這樣理解,我們設(shè)置擁有2行3列子圖的figure,然后把其中幾個子圖合并,就得到了大小不同的子圖。加入我們合并其中2個子圖,那么本來2行3列一共6個子圖就會變成5個,不信我們試試看:

    fig = plt.figure(figsize=(10, 4)) spec = fig.add_gridspec(nrows=2, ncols=6, width_ratios=[1,1,1,1,1,1], height_ratios=[1,1]) fig.suptitle('樣例3', size=20) #sub1 ax = fig.add_subplot(spec[0,0]) ax.scatter(np.random.randn(10), np.random.randn(10)) #sub2 ax = fig.add_subplot(spec[0,1]) ax.scatter(np.random.randn(10), np.random.randn(10)) #sub3 ax = fig.add_subplot(spec[:,2]) ax.scatter(np.random.randn(10), np.random.randn(10)) #sub4 ax = fig.add_subplot(spec[1,0]) ax.scatter(np.random.randn(10), np.random.randn(10)) #sub5 ax = fig.add_subplot(spec[1, 1]) ax.scatter(np.random.randn(10), np.random.randn(10))fig.tight_layout() fig = plt.figure(figsize=(10, 4)) spec = fig.add_gridspec(nrows=2, ncols=6, width_ratios=[1,1,1,1,1,1], height_ratios=[1,1]) fig.suptitle('樣例3', size=20) # sub1 ax = fig.add_subplot(spec[0,0:2]) ax.scatter(np.random.randn(10), np.random.randn(10)) # sub2 ax = fig.add_subplot(spec[0,2]) ax.scatter(np.random.randn(10), np.random.randn(10)) # sub3 ax = fig.add_subplot(spec[1:,0]) ax.scatter(np.random.randn(10), np.random.randn(10)) # sub4 ax = fig.add_subplot(spec[1,1]) ax.scatter(np.random.randn(10), np.random.randn(10)) # sub5 ax = fig.add_subplot(spec[1, 2]) ax.scatter(np.random.randn(10), np.random.randn(10))fig.tight_layout()

    給個復(fù)雜點的例子:

    fig = plt.figure(figsize=(10, 4)) spec = fig.add_gridspec(nrows=2, ncols=6, width_ratios=[2,2.5,3,1,1.5,2], height_ratios=[1,2]) fig.suptitle('樣例3', size=20) # sub1 ax = fig.add_subplot(spec[0, :3]) ax.scatter(np.random.randn(10), np.random.randn(10)) # sub2 ax = fig.add_subplot(spec[0, 3:5]) ax.scatter(np.random.randn(10), np.random.randn(10)) # sub3 ax = fig.add_subplot(spec[:, 5]) ax.scatter(np.random.randn(10), np.random.randn(10)) # sub4 ax = fig.add_subplot(spec[1, 0]) ax.scatter(np.random.randn(10), np.random.randn(10)) # sub5 ax = fig.add_subplot(spec[1, 1:5]) ax.scatter(np.random.randn(10), np.random.randn(10)) fig.tight_layout()

    2 子圖上的方法

    在 ax 對象上定義了和 plt 類似的圖形繪制函數(shù),常用的有:

    plot, hist, scatter, bar, barh, pie

    2.1 plot-線的繪制

    • 繪制任意直線
    fig, ax = plt.subplots(figsize=(4,3)) ax.plot([1,2],[2,1])

    fig, ax = plt.subplots(figsize=(4,3)) ax.plot(np.random.rand(10),np.random.rand(10)) fig, ax = plt.subplots(figsize=(4,3)) ax.axhline(0.5,0,0.8) # 0.5是y的取值;后兩個參數(shù)的意思是從左到右,占整個圖的0%的位置和百分之80%的位置 ax.axvline(0.5,0.2,0.8) # 0.5是y的取值;后兩個參數(shù)的意思是從左到右,占整個圖的20%的位置和80%的位置 ax.axline([0.3,0.3],[0.7,0.7]) # 經(jīng)過這兩個坐標(biāo)[0.3,0.3],[0.7.0.7

    2.2 hist-直方圖的繪制

    fig, ax = plt.subplots(figsize=(4,3)) ax.hist(np.random.randn(1000))


    使矩形之間產(chǎn)生間隔:

    fig, ax = plt.subplots(figsize=(4,3)) ax.hist(np.random.randn(1000),rwidth=0.9)

    2.3 對圖像的特殊設(shè)置

    • 使用 grid 可以加灰色網(wǎng)格
    fig, ax = plt.subplots(figsize=(4,3)) ax.grid(True)

    • 使用 set_xscale, set_title, set_xlabel 分別可以設(shè)置坐標(biāo)軸的規(guī)度(指對數(shù)坐標(biāo)等)、標(biāo)題、軸名
    fig, axs = plt.subplots(1, 2, figsize=(10, 4)) fig.suptitle('大標(biāo)題', size=20) for j in range(2):axs[j].plot(list('abcd'), [10**i for i in range(4)])if j==0:axs[j].set_yscale('log')axs[j].set_title('子標(biāo)題1')axs[j].set_ylabel('對數(shù)坐標(biāo)')axs[j].set_xlabel('hello 是我 你們的好朋友 xlabel')else:axs[j].set_title('子標(biāo)題1')axs[j].set_ylabel('普通坐標(biāo)') fig.tight_layout()
    • 與一般的 plt 方法類似,legend, annotate, arrow, text 對象也可以進(jìn)行相應(yīng)的繪制
    fig, ax = plt.subplots() ax.arrow(0, 0, 1, 1, head_width=0.03, head_length=0.05, facecolor='red', edgecolor='blue') ax.text(x=0, y=0,s='這是一段文字', fontsize=16, rotation=70, rotation_mode='anchor', color='green') ax.annotate('這是中點', xy=(0.5, 0.5), xytext=(0.7, 0.2), arrowprops=dict(facecolor='yellow', edgecolor='black'), fontsize=16) fig, ax = plt.subplots() ax.plot([1,2],[2,1],label="line1") ax.plot([1,1],[1,2],label="line1") ax.legend(loc=1)

    其中,圖例的 loc 參數(shù)如下:

    stringcode
    best0
    upper right1
    upper left2
    lower left3
    lower right4
    right5
    center left6
    center right7
    lower center8
    upper center9
    center10

    大部分操作都是通過axex來實現(xiàn)的,所以說axex是畫圖時最核心的部分。

    作業(yè)

    1. 墨爾本1981年至1990年的每月溫度情況

    import matplotlib.pyplot as plt from matplotlib.pyplot import MultipleLocator import pandas as pd import numpy as np plt.rcParams['font.sans-serif'] = ['SimHei'] #設(shè)置字體為SimHei顯示中文 plt.rcParams['axes.unicode_minus'] = False #設(shè)置正常顯示字符data = pd.read_csv('./layout_ex1.csv')fig, axs = plt.subplots(2,5,figsize=(20,5),sharex=True,sharey=True) fig.suptitle('墨爾本1981年至1990年月溫度曲線',size=20)index = 0 for i in range(2):for j in range(5):axs[i][j].plot(np.arange(1,13),data['Temperature'].values[index*12:(index*12+12)],'o-')axs[i][j].set_title('%s年'%data['Time'].values[index][:4])axs[i][j].xaxis.set_major_locator(MultipleLocator(1)) #把橫坐標(biāo)設(shè)置為1的倍數(shù)axs[i][j].yaxis.set_major_locator(MultipleLocator(5)) #把縱坐標(biāo)設(shè)置為5的倍數(shù)if(j==0):axs[i][j].set_ylabel('氣溫')index += 1fig.tight_layout()

    2. 畫出數(shù)據(jù)的散點圖和邊際分布

    import matplotlib.pyplot as plt import numpy as npdata = np.random.randn(2, 150) fig = plt.figure(figsize=(7,7)) spec = fig.add_gridspec(9,9,width_ratios=np.ones((9)),height_ratios=np.ones((9)))ax1 = fig.add_subplot(spec[2:9,0:7]) ax2 = fig.add_subplot(spec[0:2,0:7],sharex=ax1) # 與子圖1共享x坐標(biāo) ax3 = fig.add_subplot(spec[2:9,7:9],sharey=ax1) # 與子圖1共享y坐標(biāo)#第一個子圖 ax1.scatter(data[0],data[1]) ax1.set_ylabel('my_data_y',fontsize=10) ax1.set_xlabel('my_data_y',fontsize=10) ax1.grid(True)#第二個子圖 ax2.hist(data[0,:],rwidth=0.94) # 隱藏x軸標(biāo)度 ax2.get_xaxis().set_visible(False) # 隱藏y軸標(biāo)度 ax2.get_yaxis().set_visible(False) # 關(guān)閉邊框 for spine in ax2.spines.values():spine.set_visible(False)#第三個子圖 ax3.hist(data[0,:],rwidth=0.94, orientation='horizontal') # 隱藏x軸標(biāo)度 ax3.get_xaxis().set_visible(False) # 隱藏y軸標(biāo)度 ax3.get_yaxis().set_visible(False) # 關(guān)閉邊框 for spine in ax3.spines.values():spine.set_visible(False)fig.tight_layout()

    總結(jié)

    以上是生活随笔為你收集整理的数据可视化组队学习:《Task03 - 布局格式定方圆》笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。