数据可视化组队学习:《Task03 - 布局格式定方圆》笔记
生活随笔
收集整理的這篇文章主要介紹了
数据可视化组队学习:《Task03 - 布局格式定方圆》笔记
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 前言
- 1 子圖
- 1.1 使用 plt.subplots 繪制均勻狀態(tài)下的子圖
- 1.2 使用 GridSpec 繪制非均勻子圖
- 2 子圖上的方法
- 2.1 plot-線的繪制
- 2.2 hist-直方圖的繪制
- 2.3 對(duì)圖像的特殊設(shè)置
- 作業(yè)
前言
《第三回:布局格式定方圓》筆記。
1 子圖
1.1 使用 plt.subplots 繪制均勻狀態(tài)下的子圖
""" figsize 參數(shù)可以指定整個(gè)畫布的大小sharex 和 sharey 分別表示是否共享橫軸和縱軸刻度tight_layout 函數(shù)可以調(diào)整子圖的相對(duì)大小使字符不會(huì)重疊 """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 繪制非均勻子圖
非均勻包含兩層含義:
第一種含義:
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()第二種:
可以這樣理解,我們?cè)O(shè)置擁有2行3列子圖的figure,然后把其中幾個(gè)子圖合并,就得到了大小不同的子圖。加入我們合并其中2個(gè)子圖,那么本來2行3列一共6個(gè)子圖就會(huì)變成5個(gè),不信我們?cè)囋嚳?#xff1a;
給個(gè)復(fù)雜點(diǎn)的例子:
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 對(duì)象上定義了和 plt 類似的圖形繪制函數(shù),常用的有:
plot, hist, scatter, bar, barh, pie
2.1 plot-線的繪制
- 繪制任意直線
2.2 hist-直方圖的繪制
fig, ax = plt.subplots(figsize=(4,3)) ax.hist(np.random.randn(1000))
使矩形之間產(chǎn)生間隔:
2.3 對(duì)圖像的特殊設(shè)置
- 使用 grid 可以加灰色網(wǎng)格
- 使用 set_xscale, set_title, set_xlabel 分別可以設(shè)置坐標(biāo)軸的規(guī)度(指對(duì)數(shù)坐標(biāo)等)、標(biāo)題、軸名
- 與一般的 plt 方法類似,legend, annotate, arrow, text 對(duì)象也可以進(jìn)行相應(yīng)的繪制
其中,圖例的 loc 參數(shù)如下:
| best | 0 |
| upper right | 1 |
| upper left | 2 |
| lower left | 3 |
| lower right | 4 |
| right | 5 |
| center left | 6 |
| center right | 7 |
| lower center | 8 |
| upper center | 9 |
| center | 10 |
大部分操作都是通過axex來實(shí)現(xiàn)的,所以說axex是畫圖時(shí)最核心的部分。
作業(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ù)的散點(diǎn)圖和邊際分布
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)#第一個(gè)子圖 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)#第二個(gè)子圖 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)#第三個(gè)子圖 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 - 布局格式定方圆》笔记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 链上存证、链下传输的可信数据共享平台
- 下一篇: 基于RDMA高速网络的高性能分布式系统