Skip to main content
 首页 » 编程设计

python之具有自定义日期范围的条形图的类型错误

2024年10月17日7yxwkf

我正在尝试将数据框显示为带有 xlim 自定义日期范围的条形图。如果我选择 kind='line',我能够输出图表,但在尝试 kind='bar' 时我收到以下错误消息:

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' 

数据框如下所示:

df1 =  
     Date         Quantity 
0    2010-01-01   1   
1    2010-01-02   0 
2    2010-01-03   0 
3    2010-01-04   2 
4    2010-01-05   3 
5    2010-01-06   1 
6    2010-01-07   0 
7    2010-01-08   1 
8    2010-01-09   1 
9    2010-01-10   2 
10   2010-01-11   0 
11   2010-01-12   5 
12   2010-01-13   2 
13   2010-01-14   1 
14   2010-01-15   2 
... 

这个有效:

df1.plot(x='Date', y='Quantity', kind='line', grid=False, legend=False,  
         xlim=['2010-01-01', '2010-01-10'], figsize=(40, 16)) 

但这不是

    df1.plot(x='Date', y='Quantity', kind='bar', grid=False, legend=False,  
         xlim=['2010-01-01', '2010-01-10'], figsize=(40, 16)) 

然而,如果我从 kind='bar' 中删除 xlim,我会生成一个输出。能够输出具有自定义 x 范围的条形图会很好。

请您参考如下方法:

另一种方法怎么样 - 在绘图之前过滤您的数据?

In [10]: df.set_index('Date').ix['2010-01-01' : '2010-01-10'] 
Out[10]: 
            Quantity 
Date 
2010-01-01         1 
2010-01-02         0 
2010-01-03         0 
2010-01-04         2 
2010-01-05         3 
2010-01-06         1 
2010-01-07         0 
2010-01-08         1 
2010-01-09         1 
2010-01-10         2 
 
In [11]: df.set_index('Date').ix['2010-01-01' : '2010-01-10', 'Quantity'].plot.bar(grid=False, legend=False)