美文网首页
python统计一下自己的花费

python统计一下自己的花费

作者: 知识学者 | 来源:发表于2018-06-12 17:12 被阅读329次

    一直没有在意自己花了多少钱,我卡绑定了电话吧,取钱,支付宝什么的消费,过后都会短信通知,有时间准备统计一下,自己的情况。
    然后散点图,柱状图,折线图表示出来。
    x轴为时间,y轴为钱数。

    占位,等我有时间,把几百条短信整理出来,在作图。

    import matplotlib.pyplot as plt
    import numpy as np
    
    #x=np.arange(1,13,1)
    x=np.linspace(1,13,12,endpoint=True)
    print(x)
    t2=np.random.randint(700,1000,12)
    print(t2)
    plt.figure(figsize=(8,6))
    plt.plot(x,t2,label="money-time",color="red",linestyle="--",linewidth=1.0)
    
    #得到当前的绘图对象
    ax=plt.gca()
    ax.set_xlabel("time")
    ax.set_ylabel("money")
    ax.set_title("the time of spend money")
    ax.spines["top"].set_color("none")
    ax.spines["right"].set_color("none")
    plt.grid(True)
    plt.legend()
    plt.savefig("suiji.jpg",dpi=230)  
    plt.show()
    
    
    suiji.jpg

    花费柱状图。

    t3=np.random.randint(700,1000,12)
    x=np.arange(1,13,1)
    x_name=["Jan","feb","march","Apr","may","june","july","Aug","Sept","oct","Nov","Dece"]
    plt.bar(x,t3,color="rgb",tick_label=x_name)
    ax=plt.gca()
    #ax.hist(x_name,t3)
    ax.set_xlabel("time")
    ax.set_ylabel("money")
    ax.set_title("the time of spend money")
    ax.spines["top"].set_color("none")
    ax.spines["right"].set_color("none")
    #plt.grid(True)
    #plt.legend()
    plt.savefig("zhuzt.jpg",dpi=230)  
    plt.show()
    
    
    zhuzt.jpg

    上面是随机生成的,这次把手机短信查看 了几十条,不过短信太多了,看的马马虎虎,数据随便做了几十条,我感觉不对。

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    f=pd.read_excel("my spend.xlsx")
    f
    

    结果如下

    
              时间  金钱
    0   2016-08-27  10
    1   2016-09-04  30
    2   2016-09-05  20
    3   2016-09-30  185
    4   2016-10-12  286
    5   2016-11-11  4699
    6   2016-11-19  27
    7   2016-12-05  12
    8   2016-12-12  149
    9   2017-02-17  429
    10  2017-03-29  900
    11  2017-04-29  300
    12  2017-05-25  50
    13  2017-05-31  30
    14  2017-06-28  217
    15  2017-08-23  211
    16  2017-08-25  5200
    17  2017-09-06  210
    18  2017-09-16  118
    19  2017-09-18  300
    20  2017-09-24  50
    21  2017-09-27  30
    22  2017-09-28  300
    23  2017-10-02  300
    24  2017-10-09  40
    25  2017-10-17  500
    26  2017-10-29  310
    27  2017-10-31  505
    28  2017-11-02  120
    29  2017-11-03  300
    30  2017-11-05  406
    31  2017-11-11  80
    32  2017-11-16  46
    

    看一看,其相关数据

    sum_money=f.sum()
    print("all money:",sum_money)
    print(len(f))
    print(type(f))
    print(f.values)
    print(index)
    
    all money: 金钱    16370
    dtype: int64
    33
    <class 'pandas.core.frame.DataFrame'>
    [[Timestamp('2016-08-27 00:00:00') 10]
    
    

    金钱的消费

    t=f["金钱"]
    print(t[3])
    print(type(t))
    ax=plt.gca()
    ax.set_xlabel("time")
    ax.set_ylabel("money")
    ax.set_title("the time of spend money")
    ax.spines["top"].set_color("none")
    ax.spines["right"].set_color("none")
    plt.grid(True)
    #plt.legend()
    t.plot()
    plt.grid(True)
    plt.savefig("money.jpg",dpi=230)  
    plt.show()
    
    
    money.jpg
    f=pd.read_excel("my spend.xlsx")
    plt.plot(f.时间,f.金钱,color="r")
    ax=plt.gca()
    ax.set_xlabel("time")
    ax.set_ylabel("money")
    ax.set_title("the time of spend money")
    ax.spines["top"].set_color("none")
    ax.spines["right"].set_color("none")
    plt.grid(True)
    plt.savefig("money-time.jpg",dpi=230)  
    plt.show()
    
    

    采用excel读取数据绘制的图像为


    money-time.jpg

    总的来说失败了,我的数据没有什么用,统计自己的消费也是好玩一下。绘图好像就是这样,没有多少技巧,使用轮子。

    参考文章
    Python绘制时间序列数据的时序图、自相关图和偏自相关图
    如何用Matplotlib在Python中绘制时间?
    Python——使用matplotlib绘制柱状图

    相关文章

      网友评论

          本文标题:python统计一下自己的花费

          本文链接:https://www.haomeiwen.com/subject/bqkreftx.html