• matlib


     https://matplotlib.org/index.html

    matplotlib.pyplot  单图示例
    import matplotlib.pyplot as plt
    import random
    from pylab import mpl
    
    # 设置显示中文字体
    mpl.rcParams["font.sans-serif"] = ["SimHei"]
    # 设置正常显示符号
    mpl.rcParams["axes.unicode_minus"] = False
    
    
    x = range(60)
    y_shanghai = [random.uniform(15, 18) for i in x]
    y_beijing = [random.uniform(1,3) for i in x]
    
    # 1.创建画布
    plt.figure(figsize=(20, 8), dpi=100)
    
    # 2.绘制图像
    plt.plot(x, y_shanghai, label="上海")       
    plt.plot(x, y_beijing, color="r", linestyle="--", label="北京")
    
    # 2.1 添加x,y轴刻度
    # 构造x,y轴刻度标签
    x_ticks_label = ["11点{}分".format(i) for i in x]
    y_ticks = range(40)
    
    # 刻度显示
    plt.xticks(x[::5], x_ticks_label[::5])
    plt.yticks(y_ticks[::5])
    
    # 2.2 添加网格显示
    plt.grid(True, linestyle="--", alpha=0.5)
    
    # 2.3 添加描述信息
    plt.xlabel("时间")
    plt.ylabel("温度")
    plt.title("中午11点--12点某城市温度变化图", fontsize=20)
    
    # 2.4 图像保存
    plt.savefig("./test.png")
    
    # 2.5 添加图例
    plt.legend(loc=0)
    
    
    # 3.图像显示
    plt.show()
    

      

    matplotlib.pyplot  多图示例
    import matplotlib.pyplot as plt
    import random
    from pylab import mpl
    
    # 设置显示中文字体
    mpl.rcParams["font.sans-serif"] = ["SimHei"]
    # 设置正常显示符号
    mpl.rcParams["axes.unicode_minus"] = False

    x = range(60)
    y_shanghai = [random.uniform(15, 18) for i in x]
    y_beijing = [random.uniform(1, 5) for i in x]

    
    

    # 1.创建画布
    # plt.figure(figsize=(20, 8), dpi=100)
    fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(20, 8), dpi=100)

    
    


    # 2.绘制图像
    # plt.plot(x, y_shanghai, label="上海")
    # plt.plot(x, y_beijing, color="r", linestyle="--", label="北京")
    axes[0].plot(x, y_shanghai, label="上海")
    axes[1].plot(x, y_beijing, color="r", linestyle="--", label="北京")

    
    

    # 2.1 添加x,y轴刻度
    # 构造x,y轴刻度标签
    x_ticks_label = ["11点{}分".format(i) for i in x]
    y_ticks = range(40)

    
    

    # 刻度显示(2种方法)
    # plt.xticks(x[::5], x_ticks_label[::5])
    # plt.yticks(y_ticks[::5])
    axes[0].set_xticks(x[::5])
    axes[0].set_yticks(y_ticks[::5])
    axes[0].set_xticklabels(x_ticks_label[::5])
    axes[1].set_xticks(x[::5])
    axes[1].set_yticks(y_ticks[::5])
    axes[1].set_xticklabels(x_ticks_label[::5])

    
    

    # 2.2 添加网格显示
    # plt.grid(True, linestyle="--", alpha=0.5)
    axes[0].grid(True, linestyle="--", alpha=0.5)
    axes[1].grid(True, linestyle="--", alpha=0.5)

    
    

    # 2.3 添加描述信息

    
    

    axes[0].set_xlabel("时间")
    axes[0].set_ylabel("温度")
    axes[0].set_title("中午11点--12点某城市温度变化图", fontsize=20)
    axes[1].set_xlabel("时间")
    axes[1].set_ylabel("温度")
    axes[1].set_title("中午11点--12点某城市温度变化图", fontsize=20)

    
    

    # # 2.4 图像保存
    # plt.savefig("./test.png")

    
    

    # # 2.5 添加图例
    # plt.legend(loc=0)
    axes[0].legend(loc=0)
    axes[1].legend(loc=0)

    
    


    # 3.图像显示
    plt.show()

     

    示例

    import numpy as np
    # 0.准备数据
    x = np.linspace(-10, 10, 1000)  //等差数列  1000 为等额1000份
    y = np.sin(x)   //cos  tan   arcsin   arccos   arctan   1/tan
      
    # 1.创建画布
    plt.figure(figsize=(20, 8), dpi=100)
    
    # 2.绘制函数图像
    plt.plot(x, y)
    # 2.1 添加网格显示
    plt.grid()
    
    # 3.显示图像
    plt.show()

    图形

    折线图:以折线的上升或下降来表示统计数量的增减变化的统计图

    特点:能够显示数据的变化趋势,反映事物的变化情况。(变化)

    api:plt.plot(x, y)

    散点图:用两组数据构成多个坐标点,考察坐标点的分布,判断两变量之间是否存在某种关联或总结坐标点的分布模式。

    特点:判断变量之间是否存在数量关联趋势,展示离群点(分布规律)

    api:plt.scatter(x, y)

    柱状图:排列在工作表的列或行中的数据可以绘制到柱状图中。

    特点:绘制连离散的数据,能够一眼看出各个数据的大小,比较数据之间的差别。(统计/对比)

    api:plt.bar(x, width, align='center', **kwargs)

    绘制柱状图
    代码:
    
    # 0.准备数据
    # 电影名字
    movie_name = ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴','降魔传','追捕','七十七天','密战','狂兽','其它']
    # 横坐标
    x = range(len(movie_name))
    # 票房数据
    y = [73853,57767,22354,15969,14839,8725,8716,8318,7916,6764,52222]
    
    # 1.创建画布
    plt.figure(figsize=(20, 8), dpi=100)
    
    # 2.绘制柱状图
    plt.bar(x, y, width=0.5, color=['b','r','g','y','c','m','y','k','c','g','b'])
    
    # 2.1b修改x轴的刻度显示
    plt.xticks(x, movie_name)
    
    # 2.2 添加网格显示
    plt.grid(linestyle="--", alpha=0.5)
    
    # 2.3 添加标题
    plt.title("电影票房收入对比")
    
    # 3.显示图像
    plt.show()

    直方图:由一系列高度不等的纵向条纹或线段表示数据分布的情况。 一般用横轴表示数据范围,纵轴表示分布情况。

    特点:绘制连续性的数据展示一组或者多组数据的分布状况(统计)

    api:matplotlib.pyplot.hist(x, bins=None)

    饼图:用于表示不同分类的占比情况,通过弧度大小来对比各种分类。

    特点:分类数据的占比情况(占比)

    api:plt.pie(x, labels=,autopct=,colors)

    • 添加x,y轴刻度【知道】
      • plt.xticks()
      • plt.yticks()
      • 注意:在传递进去的第一个参数必须是数字,不能是字符串,如果是字符串吗,需要进行替换操作
    • 添加网格显示【知道】
      • plt.grid(linestyle="--", alpha=0.5)
    • 添加描述信息【知道】
      • plt.xlabel()
      • plt.ylabel()
      • plt.title()
    • 图像保存【知道】
      • plt.savefig("路径")
    • 多次plot【了解】
      • 直接进行添加就OK
    • 显示图例【知道】
      • plt.legend(loc="best")
      • 注意:一定要在plt.plot()里面设置一个label,如果不设置,没法显示
    • 多个坐标系显示【了解】
      • plt.subplots(nrows=, ncols=)
    • 折线图的应用【知道】
      • 1.应用于观察数据的变化
      • 2.可是画出一些数学函数图像
  • 相关阅读:
    【leetcode】1215.Stepping Numbers
    【leetcode】1214.Two Sum BSTs
    【leetcode】1213.Intersection of Three Sorted Arrays
    【leetcode】1210. Minimum Moves to Reach Target with Rotations
    【leetcode】1209. Remove All Adjacent Duplicates in String II
    【leetcode】1208. Get Equal Substrings Within Budget
    【leetcode】1207. Unique Number of Occurrences
    【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays
    【leetcode】LCP 3. Programmable Robot
    【leetcode】LCP 1. Guess Numbers
  • 原文地址:https://www.cnblogs.com/qj696/p/15229876.html
Copyright © 2020-2023  润新知