zoukankan      html  css  js  c++  java
  • 子图绘制

    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    '''
    ax[0,0].plot/bar/pie/hist/barh/ 直接来。两种方法都是
    有两种方法:
    方法1:
    fig = plt.figure(figsize=(10, 6), facecolor='gray')
    先生成一个图片底板,,参数可以不设置
    ax1 = fig.add_subplot(2, 2, 1)设置一个位置
    然后再绘制图
    方法2:(常用)
    fig, axes = plt.subplots(2, 2, figsize=(10, 4))
    现将图片底板准备出来,在划分好
    axes[0, 1].plot(ts,color = 'r')
    在某一个区域直接绘制
    s.plot(kind='bar',color = 'k',grid = True,alpha = 0.5,ax = axes[0]) 
    也可以用这种方式生成,不过这一种的s必须是pands数据类型
    '''
    def subplot1():
    
        fig = plt.figure(figsize=(10, 6), facecolor='gray')###############
        ax1 = fig.add_subplot(2, 2, 1)  # 第一行的左图############
        plt.plot(np.random.rand(50).cumsum(), 'k--')
        plt.plot(np.random.randn(50).cumsum(), 'b--')
        # 先创建图表figure,然后生成子图,(2,2,1)代表创建2*2的矩阵表格,然后选择第一个,顺序是从左到右从上到下
        # 创建子图后绘制图表,会绘制到最后一个子图
    
        ax2 = fig.add_subplot(2, 2, 2)  # 第一行的右图############
        ax2.hist(np.random.rand(50), alpha=0.5)
        ax3 = fig.add_subplot(2,2,3)
        s = pd.Series(np.random.randint(10))
        ax3.pie(s)
        ax4 = fig.add_subplot(2, 2, 4)  # 第二行的右图######
        df2 = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
        ax4.plot(df2, alpha=0.5, linestyle='--', marker='.')
        # 也可以直接在子图后用图表创建函数直接生成图表
        plt.show()
    def subplot2():
        fig, axes = plt.subplots(2, 2, figsize=(10, 4))
    
        ts = pd.Series(np.random.randn(1000).cumsum())
    
        print(axes, axes.shape, type(axes))
        # 生成图表对象的数组
    
        ax1 = axes[0, 1]
        ax1.fill(ts,'x')
        plt.show()
    if __name__=='__main__':
        subplot1()
  • 相关阅读:
    02-模板字符串
    01-学习vue前的准备工作
    21-z-index
    20-定位
    19-background
    18-超链接导航栏案例
    17-文本属性和字体属性
    16-margin的用法
    jBPM
    Table of Contents
  • 原文地址:https://www.cnblogs.com/yunshangyue71/p/13584390.html
Copyright © 2011-2022 走看看