Skip to main content
 首页 » 编程设计

python之Pandas dataframe.plot()之折线图之未为前两个索引绘制的系列值

2024年11月01日10小虾米

在附加图像(附加数据框和绘制的折线图快照)中 - 系列索引范围从 242.0 到 252.0,但缺少索引 242.0 和 243.0 的数据。 这里有什么问题?

相关代码如下:

import re 
import json as js 
import matplotlib.pyplot as plt 
import pandas as pd 
 
def plot(collector, run_type, stat_type): 
       ax = plt.gca() 
       run_info = collector.series[run_type]['info'] 
       dfs = [] 
       ctr_str = 'Type {}' 
       ctr = 0 
       for var in run_info: 
           ctr += 1 
           if not re.search(stat_type, var): 
               continue 
           var_info = run_info[var] 
           try: 
               dfs.append(pd.DataFrame({var: var_info['data']}, index=var_info['runs'])) 
           except: 
               print(var_info['data']) 
               print(var_info['runs']) 
               x=1 
       pd.concat(dfs, axis=1, sort=False).sort_index().plot(kind='line', ax=ax) 
       plt.show(block=True) 

请您参考如下方法:

对于 243,值出现在整列(Type1、Type2 和 Type5)中只有一个值的那些列中,因此线图不会绘制这些线。对于 242,nan 似乎打破了该列的行。但是,您需要在绘图之前处理 Nan。