zoukankan      html  css  js  c++  java
  • python-matplotlib

    # matplotlib 画图工具
    import matplotlib.pyplot as plt
    import pandas as pd
    import numpy as np
    from pandas.core.series import Series
    # 自定义数据
    X = Series(np.array([1,2,3,4,5]))
    Y = Series(np.array([1,5,2.7,3.8,4.9]))
    Y1 = (np.random.random((1,5))*10)[0]
    X1 = [1,2,3,4,5]
    
    # =================================================
    # 折线图
    # xxx 文件有两列组成 DATE(yyyy/MM/dd),VALUE
    # unrate = pd.read_csv("xxx")
    # 修改日期格式(yyyy-MM-dd)
    # unrate['DATE'] = pd.to_datetime(unrate['DATE'])
    # plt.plot() # 若不传入数据图像为空(只显示画图域)  只有 xy轴   范围为0-1 (0.0 0.2 0.4 0.6     1.0)
    # plt.plot(X,Y) # 线x轴后y轴
    # plt.xticks(rotation=45)  # 设置x轴上的数字倾斜角度
    # plt.yticks(rotation=45)  # 设置y轴上的数字倾斜角度
    # plt.xlabel('x')  # 给xy轴加标签 注意中文可能出错
    # plt.ylabel('y')
    # plt.title('title')  # 标题
    # plt.show()  # 显示图像
    # =========================================================
    # 子图操作
    # fig = plt.figure(figsize=(3,6)) # figsize画图域的长与宽
    # fig = plt.figure()
    # ax1 = fig.add_subplot(2,2,1)  # 三个画图域  第一张图 左上角序号为1
    # ax2 = fig.add_subplot(2,2,2)  # 右上角 序号2
    # ax3 = fig.add_subplot(2,2,4)  # 右下角序号4
    # 有三个画图域:一张大图  里边三个小图 ,注意尽量保持每个域 中前两个量都相同如全都是(2,2,*) 否则图像可能会重合
    #   大图分为 2*2 ,序号从左到右,上到下 1-4
    # ax1.plot(X,Y)
    # ax2.plot(X,Y)
    # =================================================
    # 在一个图上画多条线
    # fig = plt.figure()
    # plt.plot(X,Y,c="red",label="first")  # 添加label,
    # plt.plot(X1,Y1,c="blue",label="second")
    # plt.legend(loc="best")  # 可以在图中显示 不同线的标注信息
    # 指定位置 best 表示自定义最好位置, upper left 左上角  lower ,center left,right等
    # ===================================================
    # 条形图与散点图
    # plt.subplots(X,Y,0.3) # 不能此种方式直接画
    # fig,ax = plt.subplots()
    # ax.bar(X,Y,0.3) # 0.3表示柱形的宽度
    # ax.bar(X1,Y1,0.5)  # X1 表示的是每个柱距离0的大小 Y1是柱的高度
    # ax.set_xticklabels(["a","b","c","d","e"],rotation=45) # 定义x轴要显示的内容 本例为1,2,3,4,5
    # ax.set_xticks([1,2,3,4,5]) # 指明x轴上1,2,3,4,5那些需要显示
    # ax.set_ylabel("y")  # y轴标签
    # ax.set_xlabel("x")  # x轴标签
    # ax.set_title("title")
    # -------------------------------
    # 横向柱状图
    # fig,ax = plt.subplots()
    # ax.barh(X1,Y1,0.3) # x 变为竖着的
    # ax.set_yticks()
    # ax.set_yticklabels(yy)
    # ===================================================
    # fig,ax = plt.subplots()
    # ax.scatter(X1,Y1)
    # ax.set_xlabel("X")
    # ax.set_ylabel("Y")
    # ---------------------
    # 子图
    # fig = plt.figure()
    # ax1 = fig.add_subplot(2,1,1)
    # ax2 = fig.add_subplot(2,1,2)
    # ax1 ... ax2 ...
    
    # -----------------
    # 直方图
    # fig,ax = plt.subplots()
    # ax.hist(Y1)
    # ax.hist([1,1,1,3,3,3,4,4,4,5,5,5,5,5,5,51])
    # ax.set_ylim(0,3) # 设置y轴区间大小
    # ax.set_xlim(0,5) # 设置y轴区间大小
    # 接受一组数据,假如数据是可比较的,
    # 在x轴上会分区间显示, 数值大小在改区间的数的个数  区间个数 通过bins修改
    # 通过range=(2,5) 控制x轴显示的范围  只显示2到5范围内的
    # ======================================================
    # 盒图
    # fig,ax = plt.subplots()
    # ax.boxplot([[1,2,3],2,3,4,5,6,7])
    # ax.set_xticklabels(["a","b"],rotation=90) # x轴上显示的标签
    # ax.set_ylim(0.5) # 范围
    # boxplot([]) list中可为几个数值,也可以为[] ,即每个[] 对应x上一项
    # 注意最多两层 [[],[]] 三层报错
    # 图的主要作用是 容易看出一个[] 中四等分出的值
    #  a--> 1,1.5,2,2.5,3   b-->2 ....
    # ====================================
    
    # fig,ax = plt.subplots()
    # ax.tick_params(bottom="on")
    # for key,spine in ax.spines.items():
    #     spine.set_visible(False)
    # ax.plot(X,Y)
    # ax.plot(X1,Y1)
    
    # 细节设置
    # ax.plot(c=(0/255,10/255,164/255),linewidth=3) # 设置宽度,颜色:颜色使用元祖方式表示,固定格式
    # ax.text(x,y,"zz") # 在x,y点处添加zz字符
    # plt.show()
    import matplotlib.pyplot as plt
    import json
    import numpy as np
    import matplotlib
    font = {'family': 'MicroSoft YaHei','weight': 'bold','size': 10} # 中文乱码
    matplotlib.rc("font", **font)
    file = r'temp2.txt'
    car = []
    bus = []
    van = []
    others = []
    X = []
    with open(file,'r') as f:
        data = [json.loads(line) for line in f.readlines()]
    for i,line in enumerate(data):
        X.append(i)
        temp = line.get(str(i))
        car.append(temp.get('car'))
        bus.append(temp.get('bus'))
        van.append(temp.get('van'))
        others.append(temp.get('others'))
    X = np.divide(X,60)
    plt.plot()
    plt.plot(X,car,label='car')
    plt.plot(X,bus,label='bus')
    plt.plot(X,van,label='van')
    plt.plot(X,others,label='other')
    plt.plot(X,np.sum([car,van,bus,others],axis=0),label='sum')
    plt.legend(loc=2)
    plt.xlabel('运行时间(分钟)')
    plt.ylabel('数量')
    plt.title('车辆流统计')
    plt.show()
    

      

  • 相关阅读:
    Python基础(三) 基本数据类型②:列表
    Python基础(一) Python3环境搭建
    Python基础(二) 基本数据类型①:整数型、布尔型、字符串
    win7下花生壳无法访问的问题
    Java中使用Hhibernate遇到一个错误:No Dialect mapping for JDBC type:1
    ASP.NET MVC2 实验总结
    js 格式化时间
    web service
    初学。。ASP.NET
    在.NET中使用XML的基本方法
  • 原文地址:https://www.cnblogs.com/Dean0731/p/11586445.html
Copyright © 2011-2022 走看看