• matplotlib


    折线图

    from matplotlib import pyplot as plt, font_manager
    import random
    
    x = range(2, 26, 2)
    y = [14, 14.5, 15, 16, 18, 20, 22, 23, 24, 25, 17, 13]
    # 图片尺寸, figsize宽20高8, dpi每英寸像素
    plt.figure(figsize=(20, 8), dpi=80)
    plt.plot(x, y)
    # x轴刻度
    plt.xticks(x)   # 刻度为range步长2
    plt.yticks(range(min(y), max(y)+1))
    plt.savefig('./sig_size.png')
    plt.savefig('./sig_size.svg')
    plt.show()

    from matplotlib import pyplot as plt, font_manager
    import random
    
    x = range(0, 120)
    y = [random.randint(20, 35) for i in range(120)]
    plt.figure(figsize=(20, 8), dpi=80)
    plt.plot(x, y)
    _x = list(x)[::10]  # x轴刻度步长10
    _xtick_labels = ['hello, {}'.format(i) for i in _x]
    plt.xticks(_x, _xtick_labels)
    plt.show()

    from matplotlib import pyplot as plt, font_manager
    import random
    
    # 设置matplotlib中文字体,命令行fc-list 支持的字体, fc-list :lang=zh 支持的中文字体
    my_font = font_manager.FontProperties(fname='/System/Library/Fonts/STHeiti Medium.ttc')
    x = range(0, 120)
    y = [random.randint(20, 35) for i in range(120)]
    y_2 = [random.randint(10, 20) for j in range(120)]
    plt.figure(figsize=(20, 8), dpi=80)
    plt.plot(x, y, label='杭州', color='green', linestyle='--', linewidth=5, alpha=0.8)
    # 生成多条数据时多次运行plt.plot命令, linestyle线条风格, linewidth线条粗细, alpha线条透明度
    plt.plot(x, y_2, label='云南', color='orange')
    _xtick_labels = ['10点{}分'.format(i) for i in range(60)]
    _xtick_labels += ['11点{}分'.format(i) for i in range(60)]
    # _x和_xtick_labels数据长度一样。刻度取步长,步长也要相同
    _x = list(x)[::5]
    _xtick_labels = _xtick_labels[::5]
    plt.xticks(_x, _xtick_labels, rotation=45, fontproperties=my_font)
    # 添加描述信息
    plt.xlabel('时间', fontproperties=my_font)
    plt.ylabel('温度 单位(摄氏度)', fontproperties=my_font)
    plt.title('10点到12点每分钟气温变化', fontproperties=my_font)
    # 绘制网格, alpha透明度
    plt.grid(alpha=0.4)
    # 添加图例区分数据是杭州还是云南, loc图例位置, loc=0, upper left,...
    plt.legend(prop=my_font, loc=0)
    plt.show()

     

    条形图

    from matplotlib import pyplot as plt, font_manager
    
    my_font = font_manager.FontProperties(fname='/System/Library/Fonts/STHeiti Medium.ttc')
    
    # 基础条形图
    a = ['战狼2', '速度与激情8', '功夫瑜伽', '西游伏妖篇', '变形金刚5', '摔跤吧!爸爸', '加勒比海盗5', '金刚', '极限特工', '生化危机6', '乘风破浪', '神偷奶爸3', '智取威虎山', '大闹天竺', '金刚狼3', '蜘蛛侠', '悟空传', '银河护卫队2', '情圣', '新木乃伊']
    b = [56.01, 26.94, 17.53, 16.49, 15.45, 12.96, 11.8, 11.61, 11.28, 11.12, 10.49, 10.3, 8.75, 7.55, 7.32, 6.99, 6.88, 6.86, 6.58, 6.23]
    
    plt.figure(figsize=(20, 8), dpi=80)
    # 条形图竖向
    plt.bar(range(len(a)), b, width=0.3)
    # 设置字符串到x轴
    plt.xticks(range(len(a)), a, fontproperties=my_font, rotation=45)
    plt.show()

    from matplotlib import pyplot as plt, font_manager
    
    my_font = font_manager.FontProperties(fname='/System/Library/Fonts/STHeiti Medium.ttc')
    
    # 基础条形图
    a = ['战狼2', '速度与激情8', '功夫瑜伽', '西游伏妖篇', '变形金刚5', '摔跤吧!爸爸', '加勒比海盗5', '金刚', '极限特工', '生化危机6', '乘风破浪', '神偷奶爸3', '智取威虎山', '大闹天竺', '金刚狼3', '蜘蛛侠', '悟空传', '银河护卫队2', '情圣', '新木乃伊']
    b = [56.01, 26.94, 17.53, 16.49, 15.45, 12.96, 11.8, 11.61, 11.28, 11.12, 10.49, 10.3, 8.75, 7.55, 7.32, 6.99, 6.88, 6.86, 6.58, 6.23]
    
    plt.figure(figsize=(20, 8), dpi=80)
    # 条形图横向
    plt.barh(range(len(a)), b, height=0.3)
    plt.yticks(range(len(a)), a, fontproperties=my_font)
    plt.grid(alpha=0.3)
    plt.show()

    from matplotlib import pyplot as plt, font_manager
    
    my_font = font_manager.FontProperties(fname='/System/Library/Fonts/STHeiti Medium.ttc')
    
    # 电影票房9.14, 9.15, 9.16对应的票房条形图
    a = ['星球崛起3', '敦刻尔克', '蜘蛛侠', '战狼2']
    b_14 = [2358, 399, 2358, 362]
    b_15 = [12357, 156, 2045, 168]
    b_16 = [15746, 312, 4497, 319]
    bar_width = 0.2
    x_14 = list(range(len(a)))
    x_15 = [i+bar_width for i in x_14]
    x_16 = [i+bar_width for i in x_15]
    plt.figure(figsize=(20, 8), dpi=80)
    plt.bar(x_14, b_14, width=bar_width, label='9月14日')
    plt.bar(x_15, b_15, width=bar_width, label='9月15日')
    plt.bar(x_16, b_16, width=bar_width, label='9月16日')
    plt.xticks(x_15, a, fontproperties=my_font)
    plt.xlabel('电影', fontproperties=my_font)
    plt.ylabel('票房 单位(万)', fontproperties=my_font)
    plt.title('9.14, 9.15, 9.16票房', fontproperties=my_font)
    plt.legend(prop=my_font)
    plt.show()

      

    散点图

    from matplotlib import pyplot as plt, font_manager
    
    my_font = font_manager.FontProperties(fname='/System/Library/Fonts/STHeiti Medium.ttc')
    
    y_3 = [11, 17, 16, 11, 12, 11, 12, 6, 6, 7, 8, 9, 12, 15, 14, 17, 18, 21,16, 17, 20, 14, 15, 15, 15, 19, 21, 22, 22, 22, 23]
    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]
    x_3 = range(1, 32)
    x_10 = range(51, 82)
    
    plt.figure(figsize=(20, 8), dpi=80)
    plt.scatter(x_3, y_3, label='3月份')
    plt.scatter(x_10, y_10, label='10月份')
    
    _x = list(x_3) + list(x_10)
    _xtick_labels = ['3月{}日'.format(i) for i in x_3] + ['10月{}日'.format(i) for i in x_10]
    plt.xticks(_x[::3], _xtick_labels[::3], fontproperties=my_font, rotation=45)
    plt.xlabel('时间', fontproperties=my_font)
    plt.ylabel('温度 单位(摄氏度)', fontproperties=my_font)
    plt.title('标题', fontproperties=my_font)
    plt.legend(prop=my_font, loc='upper left')
    plt.show()

    直方图

    from matplotlib import pyplot as plt
    
    a = [116, 103, 138, 123, 126, 102, 139, 86, 109, 82, 134, 104, 131, 144, 83, 150, 106, 119, 87, 126, 106, 95, 81, 133, 118, 92, 140, 90, 137, 87, 106, 132, 80, 133, 115, 85, 96, 85, 138, 105, 106, 82, 96, 148, 123, 105, 107, 127, 88, 136, 136, 89, 107, 86, 122, 116, 135, 98, 146, 120, 114, 103, 92, 126, 137, 126, 92, 111, 122, 97, 104, 123, 132, 135, 119, 125, 141, 124, 114, 88, 148, 98, 96, 93, 91, 113, 147, 138, 125, 107, 128, 81, 126, 134, 86, 101, 131, 114, 138, 122, 108, 87, 92, 104, 146, 116, 108, 80, 115, 106, 118, 88, 103, 127, 117, 124, 96, 116, 116, 117]
    
    # 计算组数
    d = 5   # 组距
    num_bins = (max(a) - min(a)) // d   # 保证(max(a) - min(a)) // d为整数,要和组距d相互调整
    
    plt.figure(figsize=(20, 8), dpi=80)
    plt.hist(a, num_bins, density=True)  # density=True y轴直方图频率
    plt.xticks(range(min(a), max(a)+d, d))
    plt.grid()
    plt.show()

    具体复杂matplotlib示例官网https://matplotlib.org/gallery/index.html

  • 相关阅读:
    Dev C++ 工程没有调试信息 解决办法
    写一个函数,输入一个二叉树,树中每个节点存放了一个整数值,函数返回这棵二叉树中相差最大的两个节点间的差值绝对值。请注意程序效率。
    oracle10g登录em后,提示“java.lang.Exception: Exception in sending Request :: null”
    网站登录的破解
    sql 日志恢复
    Oracle expdp/impdp 使用示例
    Oracle数据库备份和恢复的基本命令
    检索 COM 类工厂中 CLSID 为 {{10020200-E260-11CF-AE68-00AA004A34D5}} 的组件时失败解决办法
    win7访问部分win2003速度慢
    公交车路线查询系统后台数据库设计--换乘算法改进与优化
  • 原文地址:https://www.cnblogs.com/xuyaping/p/13564705.html
Copyright © 2020-2023  润新知