Task03

准备

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    plt.rcParams['font.sans-serif'] = ['SimHei']   #用来正常显示中文标签
    plt.rcParams['axes.unicode_minus'] = False   #用来正常显示负号

子图(多图)

    #### 均匀子图
    法一:plt.subplots画子图()
    样例:
    
    fig, axs = plt.subplots(2, 5, figsize=(10, 4), sharex=True, sharey=True) # 2,5分别是画布中子图的行列,不传入时默认为1,figsize可以指定整个画布的大小,sharex 和 sharey 分别表示是否共享横轴和纵轴刻度,tight_layout可以调整子图的相对大小使字符不会重叠
    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)) #np.random.randn(dn) --> 返回一个或一组样本,dn是维度
            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('横坐标')
            if j==0: axs[i][j].set_ylabel('纵坐标')
    fig.tight_layout() # tight_layout可以调整子图的相对大小使字符不会重叠
    法二:基于pyplot模式的写法
    样例:
    
    plt.figure() #创建画布
    plt.subplot(2,2,1)  # 子图1 ,总行数,总列数,当前子图的index-->排序
    plt.plot([1,2], 'r')
    plt.subplot(2,2,2) # 子图2
    plt.plot([1,2], 'b')
    plt.subplot(224)  #子图3,三位数都小于10时,可省略逗号,命令等价于plt.subplot(2,2,4) 
    plt.plot([1,2], 'g');
    
    创建极坐标系的图
    N = 150
    r = 2 * np.random.rand(N)
    theta = 2 * np.pi * np.random.rand(N) #2派R
    area = 200 * r**2
    colors = theta
    plt.subplot(projection='polar')
    plt.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75); # 正式画图
    
    思考1借鉴:
    (https://blog.csdn.net/weixin_47759089/article/details/123670063)
    fig = plt.figure(figsize=(10, 6))
    ax = plt.subplot(111, projection='polar')
    ax.set_theta_direction(-1)  
    ax.set_theta_zero_location('N')
    r = np.arange(100, 800, 20)
    theta = np.linspace(0, np.pi * 2, len(r), endpoint=False)
    ax.bar(theta, r, width=0.18, color=np.random.random((len(r), 3)),
           align='edge', bottom=100)
    ax.text(np.pi * 3 / 2 - 0.2, 90, '极坐标系', fontsize=14)  
    for angle, height in zip(theta, r):
        ax.text(angle + 0.03, height + 120, str(height), fontsize=height / 80)
    plt.axis('off')  
    plt.tight_layout()  
    plt.show()
    #### 非均匀子图 -- > 1.图的比例大小不同但没有跨行或跨列;2.图为跨列或跨行状态
    样例:
    画法一:
    
    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]) # add_gridspec 可以指定相对宽度比例 width_ratios 和相对高度比例参数 height_ratios
    fig.suptitle('样例2', size=20)
    for i in range(2):
        for j in range(5):
            ax = fig.add_subplot(spec[i, j])
            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('横坐标')
            if j==0: ax.set_ylabel('纵坐标')
    fig.tight_layout()
    画法二:
    
    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()
    
    #### 补充
    补充1:
    fig, ax = plt.subplots(figsize=(4,3))
    ax.axhline(0.5,0.2,0.8) # 水平画线
    ax.axvline(0.5,0.2,0.8) # 垂直画线
    ax.axline([0.3,0.3],[0.7,0.7]); # 任意方向画线
    补充2:
    fig, ax = plt.subplots(figsize=(4,3))
    ax.grid(True) # grid加灰色网格
    补充3:
    fig, axs = plt.subplots(1, 2, figsize=(10, 4))
    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') # 设置坐标轴的规度(指对数坐标等)
        else:
            pass
    fig.tight_layout()

思考题(未画出)
墨尔本1981年至1990年的每月温度情况
ex1 = pd.read_csv(‘data/layout_ex1.csv’)
ex1.head()
Time Temperature
0 1981-01 17.712903
1 1981-02 17.678571
2 1981-03 13.500000
3 1981-04 12.356667
4 1981-05 9.490323
请利用数据,画出如下的图:
在这里插入图片描述
画出数据的散点图和边际分布
用 np.random.randn(2, 150) 生成一组二维数据,使用两种非均匀子图的分割方法,做出该数据对应的散点图和边际分布图
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值