【问题标题】:Plot Category Percentages Over Time随着时间的推移绘制类别百分比
【发布时间】:2018-07-17 08:39:31
【问题描述】:

我有一个带有日期时间索引和软件版本列的 DataFrame:

Date                   Version
2018-07-10 15:42:16    1.0
2018-07-10 16:38:18    1.0
2018-07-10 20:21:54    2.0
2018-07-11 08:28:56    1.0
2018-07-11 13:16:48    2.0
2018-07-13 15:25:56    2.0

我想按时间(比如每月)绘制我有多少个唯一版本以及这些版本是什么。我希望随着时间的推移将其绘制为填充区域图。随着采用过程中的区域增加,新版本推出时区域减少。

可能是这样,但以横向时间和版本为分组。

https://python-graph-gallery.com/253-control-the-color-in-stacked-area-chart/

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    你可以这样做:

    df.groupby('Version').resample('M').nunique()
    

    或:

    df.resample('d')['Version'].unique()
    

    【讨论】:

      【解决方案2】:

      试试这个,假设你的数据文件格式如上:

      import numpy as np
      import pandas as pd 
      import matplotlib.pyplot as plt 
      dateparse = lambda x: pd.datetime.strptime(x, '%Y-%m-%d %H:%M:%S') 
      data = pd.read_table(filename, parse_dates=['Date'], date_parser=dateparse)  # your file name 
      data['month']  = data['Date'].dt.month.values; # can be year, etc. 
      Months = data.groupby('month')['Version'].nunique().index.values 
      nVersion = data.groupby('month')['Version'].nunique().values  
      plt.fill_between(Months,0,nVersion)
      plt.show()
      

      【讨论】:

        猜你喜欢
        • 2021-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-24
        相关资源
        最近更新 更多