创建一个包含Pandas中所有日期的绘图

2024-04-20 11:39:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要创建一个带有多个列的数据帧的绘图。我会在x轴上写日期。怎么可能使一个地块上有所有的日期?现在我的数据显示为每五个月一次。在某些列中,数据非常小,但它们在绘图上可见对我来说非常重要。我的数据框看起来像这样。你知道吗

    date      col1        col2      col3     col4        col5        col6
20.05.2016  1091,06     12932,31       0    9343,334    23913,74      0
27.05.2016  1086,66     11845,64       0    9786,654    23913,74      0
03.06.2016  1083,04     10762,59       0    9786,654    23913,74      0
10.06.2016  1083,96     9678,630    4000    9786,654    23913,74      0
17.06.2016  1087,31     22718,40       0    9786,654    23913,74   1412
24.06.2016  1089,78     21628,62       0    9786,654    23828,96      0
01.07.2016  1083,70     20544,92       0    9749,567    23828,96      0
08.07.2016  1081,92     19463          0    9749,567    23828,96      0
...

我的代码如下所示:

df.plot(figsize=(20,10), x='date', y=['col1', 'col2', 'col3', 'col4', 'col5', 'col6'])

plt.show()

如果您有任何建议,我将不胜感激。你知道吗


Tags: 数据代码绘图dfdateplotcol2col3
1条回答
网友
1楼 · 发布于 2024-04-20 11:39:06

首先使用^{},然后如果需要子集[],如果需要按名称筛选列:

cols = ['col1', 'col2', 'col3', 'col4', 'col5', 'col6']
df.set_index('date')[cols].plot(figsize=(20,10))

对于df的所有列,省略它:

df.set_index('date').plot(figsize=(20,10))

但是如果需要不带0的所有列,请使用^{}^{},并按^{}!=)和^{}对每列的所有True进行筛选:

#replace decimals , to . and then to floats, check notice for another solution 

df['Date'] = pd.to_datetime(df['Date'])
df = df.set_index('date').replace(',', '.', regex=True).astype(float)

print (df.ne(0))
            col1  col2   col3  col4  col5   col6
date                                            
2016-05-20  True  True  False  True  True  False
2016-05-27  True  True  False  True  True  False
2016-03-06  True  True  False  True  True  False
2016-10-06  True  True   True  True  True  False
2016-06-17  True  True  False  True  True   True
2016-06-24  True  True  False  True  True  False
2016-01-07  True  True  False  True  True  False
2016-08-07  True  True  False  True  True  False

print (df.ne(0).all())
col1     True
col2     True
col3    False
col4     True
col5     True
col6    False
dtype: bool

df = df.loc[:, df.ne(0).all()]
print (df)
               col1      col2      col4      col5
date                                             
2016-05-20  1091.06  12932.31  9343.334  23913.74
2016-05-27  1086.66  11845.64  9786.654  23913.74
2016-03-06  1083.04  10762.59  9786.654  23913.74
2016-10-06  1083.96   9678.63  9786.654  23913.74
2016-06-17  1087.31  22718.40  9786.654  23913.74
2016-06-24  1089.78  21628.62  9786.654  23828.96
2016-01-07  1083.70  20544.92  9749.567  23828.96
2016-08-07  1081.92  19463.00  9749.567  23828.96


df.plot(figsize=(20,10))

注意:

小数也有问题,所以需要参数decimal^{}replaceastype在上面的解决方案中使用:

df = pd.read_csv('filename', index_col=['date'], decimal=',', parse_dates=['date'])

print (df)
               col1      col2  col3      col4      col5  col6
date                                                         
2016-05-20  1091.06  12932.31     0  9343.334  23913.74     0
2016-05-27  1086.66  11845.64     0  9786.654  23913.74     0
2016-03-06  1083.04  10762.59     0  9786.654  23913.74     0
2016-10-06  1083.96   9678.63  4000  9786.654  23913.74     0
2016-06-17  1087.31  22718.40     0  9786.654  23913.74  1412
2016-06-24  1089.78  21628.62     0  9786.654  23828.96     0
2016-01-07  1083.70  20544.92     0  9749.567  23828.96     0
2016-08-07  1081.92  19463.00     0  9749.567  23828.96     0

相关问题 更多 >