1.dataframe用时间分组细节,如果索引是时间类型,那么,可以用下面的代码指定分组的时间段
agg_10m = df.groupby(pd.TimeGrouper(freq='10Min')).aggregate(numpy.sum)
这样的话就不用再自己处理索引数据了。
2.今天又搜了一下画动态图的库,我想展示动态图,把数据“播放”出来,plotly.express是支持动态图的,但是有很多限制,而且只绘制当前状态,总之看起来很奇怪,另一个是pandas_alive,但是因为库版本冲突没有安装成功。它展示的是短视频里看到的那种条形图的排名,然后有那种过渡动画的那种,以后再研究。第三个是matplotlib,这个成熟的绘图库功能还是很强大的。其实bokeh也是支持的,之前看过文档,但是没有做实验。还有一个就是pyecharts,看效果还行,但不是我想要的。
- import plotly.express as px
- import numpy as np
- from datetime import datetime
- import time
- from pandas.core import resample as rp
- def convert_time(df,d):
- try:
- date_str = d + " 00:00:00"
- struct_time = time.strptime(date_str, '%Y-%m-%d %H:%M:%S')
- timesamp = time.mktime(struct_time)
- res = timesamp + df
- d = datetime.fromtimestamp(res)
- year = d.year
- month = d.month
- day = d.day
- hour = d.hour
- minute = d.minute
- return str(datetime(year, month, day, hour, minute))
- except:
- return df
- df = MDF("C:\\Users\\gw00305123\\Desktop\\下载\\AnalysisTools\\A样耐久稳态工况_3th cycle_2022-06-24 11-05-13.MDF").to_dataframe()
- date_str = '2022-06-24'
- df['myindex'] = np.arange(0,df.shape[0])
- n_list = [
- 'sAPT12_CatIn_pAir_kPa',
- 'spABPV_CatIn_pAir_kPa',
- 'sABPG_ABPV_posVlv_perc',
- 'csABPV_posVlv_perc',
- 'sAFMM_AcIn_mdotAir_gps',
- 'spAC_CatIn_mdotAir_gps',
- 'FeedbackSpeed',
- 'csAC_nMotor_rpm',
- 'sFPT12_AnIn_pHy_kPa',
- 'spFIV_AnIn_pHy_kPa',
- 'csFIV_DutCy_perc',
- 'FRB_Speed',
- 'csFRB_Spd_rpm',
- 'sCTE12_StkIn_tClt_dC',
- 'sCTE21_StkOut_tClt_dC',
- 'sCWP_nMotor_rpm',
- 'sCPT21_StkOut_pClt_kPa',
- 'sFPT21_AnOut_pHy_kPa',
- 'sCPT12_StkIn_pClt_kPa',
- 'sCVM_uMeanCell_V',
- 'sCVM_uMinCell_V',
- 'sCVM_uMeanCell_V',
- 'sCVM_uMinCell_V',
- 'sPDU_StkOut_iSTK_A',
- 'myindex',
- ]
- df = df.loc[:,n_list]
- df.reset_index(inplace=True)
- if 'Time' not in df and 'timestamps' in df:
- df['Time'] = df['timestamps'].apply(convert_time,args=([date_str]))
-
- #df = df.set_index('Time')
- #group = df.groupby(rp.TimeGrouper(freq='10Min'))
-
- df = df.dropna()
- #df = df.iloc[10000:11000,:]
- px.bar(df, y='sFPT21_AnOut_pHy_kPa',animation_frame='Time',animation_group='Time',color='Time',range_x=[0,50000],range_y=[0,500])
这个是我研究时的代码,画出的图很奇怪,还没能达到要求。
matplotlib案例,案例原文Python实现动态排序图 Python编程实现超炫动态排序图(IT技术) (qb5200.com)
- # 导入库函数
- import random
- import pandas as pd
- import matplotlib.pyplot as plt
- import matplotlib.ticker as ticker
- import matplotlib.animation as animation
- from IPython.display import HTML
- import matplotlib
- #防止动漫内存太大,报错
- matplotlib.rcParams['animation.embed_limit'] = 2**128
-
- #pandas读取数据,且去列名分别为name,group,year和value的值;
- url = 'https://gist.githubusercontent.com/johnburnmurdoch/4199dbe55095c3e13de8d5b2e5e5307a/raw/fa018b25c24b7b5f47fd0568937ff6c04e384786/city_populations'
- df = pd.read_csv(url, usecols=['name', 'group', 'year', 'value'])
- df.head()
-
- #导入random函数,randomcolor用于生成颜色代码
- # randomcolor生成颜色代码原理,
- # 【1-9/A-F】15个数字随机组合成6位字符串前面再加上一个“#”号键
- import random
- def randomcolor():
- colorlist = ['1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
- color =''
- for i in range(6):
- color += random.choice(colorlist)
- return '#'+ color
-
- #对地区列表进行去重,分类;
- area_list1 = set(df['name'])
- # color_list用于存放随机生成颜色代码个数
- # 因为后面区域个数 要与颜色个数保持一致,这里用了len函数;
- color_list =[]
- for i in range(len(area_list1)):
- str_1 = randomcolor()
- color_list.append(str_1)
- str_1 = randomcolor()
- #area_list转化为列表
- area_list_1 = [i for i in area_list1]
- print(color_list)
- print(area_list_1)
-
- #colors表示 所在城市:颜色 一一对应字典形式;
- colors =dict(zip(area_list_1,color_list))
- print(colors)
- #group_lk为 城市:所在区域 --对应字典形式;
- group_lk = df.set_index('name')['group'].to_dict()
- print(group_lk)
-
- # 用plt加理图表,figsize表示图标长宽,ax表示标签
- fig, ax = plt.subplots(figsize=(15, 8))
- #dras_barchart生成current_year这一年各城市人口基本情况;
- def draw_barchart(current_year):
- #dff对year==current_year的行,以value从升序方式排序,取后十名也就是最大值;
- dff = df[df['year'].eq(current_year)].sort_values(by='value',ascending = True).tail(12)
- # 所有坐标、标签清除
- ax.clear()
- #显示颜色、城市名字
- ax.barh(dff['name'],dff['value'],color = [colors[x] for x in dff['name']])
- dx = dff['value'].max()/200
- #ax.text(x,y,name,font,va,ha)
- # x,y表示位置;
- # name表示显示文本;
- # va,ba分别表示水平位置,垂直放置位置;
- for i ,(value,name) in enumerate(zip(dff['value'], dff['name'])):
- ax.text(value-dx,i,name,size=14,weight=600,ha ='right',va = 'bottom')
- ax.text(value-dx,i-.25,group_lk[name],size = 10,color ='#444444',ha ='right',va = 'baseline')
- ax.text(value+dx,i ,f'{value:,.0f}',size = 14,ha = 'left',va ='center')
- #ax.transAxes表示轴坐标系,(1,0.4)表示放置位置
- ax.text(1,0.4,current_year,transform = ax.transAxes,color ='#777777',size = 46,ha ='right',weight=800)
- ax.text(0,1.06,'Population (throusands)',transform = ax.transAxes,size=12,color='#777777')
- #set_major_formatter表示刻度尺格式;
- ax.xaxis.set_major_formatter(ticker.StrMethodFormatter('{x:,.0f}'))
- ax.xaxis.set_ticks_position('top')
- ax.tick_params(axis='x',colors='#777777',labelsize=12)
- ax.set_yticks([])
- #margins表示自动缩放余额;
- ax.margins(0,0.01)
- # 设置后面的网格
- ax.grid(which='major',axis='x',linestyle='-')
- #刻度线和网格线是在图标上方还是下方,True为下方
- ax.set_axisbelow(True)
- ax.text(0,1.15,'The most population cities in the word from 1500 to 2018',
- transform=ax.transAxes,size=24,weight=600,ha='left',va='top')
- ax.text(1,0,'by@zeroing1',transform = ax.transAxes,color ='#777777',ha = 'right',
- bbox = dict(facecolor='white',alpha = 0.8,edgecolor='white'))
- #取消图表周围的方框显示
- plt.box(False)
-
- #绘制2018年各城市人口情况
- draw_barchart(2018)