具有自定义日期范围的条形图的TypeError

时间:2016-09-26 19:50:52

标签: python pandas matplotlib plot dataframe

我正在尝试将数据框显示为带有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))

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

1 个答案:

答案 0 :(得分:2)

替代方法怎么样 - 在绘图之前过滤数据

class Car(models.Model):
    manufacturer = models.ForeignKey(
        'production.Manufacturer',
        on_delete=models.CASCADE,
)