开发者社区> 问答> 正文

“ValueError:num必须是1 <= num <= 0,而不是1”试图绘制Pandas DataFrame的直方图?

一码平川MACHEL 2019-01-21 13:52:54 1871

我有一个pandas.DataFrame列中的一个'Time',包含datetime对象:

In [3]: df['Time']
Out[3]:
req_id
d95bc740-d320-4d80-b851-81b7b7758a8f 2019-01-17 23:29:43.043
0df512ee-094c-46ce-a041-a6a9c90fddef 2019-01-17 23:26:29.464
e87807b3-b681-4343-8ba3-9e35df99f2d5 2019-01-17 23:17:34.659
cc638d8a-947d-40ec-8488-9292f801eb5b 2019-01-17 23:15:26.953
b710d0e8-7e51-49c1-9bda-6abca2794fa8 2019-01-17 23:15:05.375
142fbb7a-6472-4d4e-a5b9-5a4a4d4ae747 2019-01-17 23:14:51.050
6a0dd117-6d1e-4a2e-9efa-f2cac73b1aa0 2019-01-17 23:14:26.762
03ed901e-f6fb-47b3-bcea-d4afa7432bd3 2019-01-17 23:12:10.766
1b6c8326-518e-4c62-8a72-84052cd6f024 2019-01-17 23:11:30.688
d4f3144a-56d8-438d-bc0d-4e8d0afee2ff 2019-01-17 23:10:10.456
43976955-f32c-494b-b7f4-5c04abc2108a 2019-01-17 23:10:07.456
1f8a88d2-e4fd-437f-852f-026ed0eb30e7 2019-01-17 23:09:48.396
7fc550a1-2df1-4839-b5fd-59cf1d6c9c0e 2019-01-17 23:09:28.372
2cd8481e-79ab-4491-bcac-18761b9381ce 2019-01-17 23:09:08.752
18b2dc35-fae6-4e75-abd2-5bdadd8a1079 2019-01-17 23:08:14.430
43cc41f4-18cf-4854-8c78-89805f902dbf 2019-01-17 23:05:32.962
f27e1c06-4f69-4a76-bb80-49799b897edb 2019-01-17 23:04:33.882
c641e2d1-4560-406f-a5b0-46e9bd27207d 2019-01-17 23:03:43.959
f3726c23-d3ae-4a87-94b0-6462f9de733c 2019-01-17 23:03:27.322
e0fb0974-fc1f-48b6-a7b5-b647e0d33fd2 2019-01-17 23:03:06.342
c968bbd4-6e44-4920-a4a3-d5b7632f232d 2019-01-17 23:03:01.342
41b811f8-98ca-4cd3-97b8-ab9c6287ab98 2019-01-17 23:00:47.052
ca6d86cd-7293-41c2-a976-c70403dca18e 2019-01-17 22:58:58.446
97a83d4d-a847-4953-b411-49847b821683 2019-01-17 22:58:50.723
05ccb053-a56a-42bd-acec-08fddc71c26b 2019-01-17 22:58:03.835
b5af2bbb-9121-4d5f-bf30-6310c5b88584 2019-01-17 22:57:01.791
839541a4-5ae6-4081-bff0-c3799beda90f 2019-01-17 22:56:40.152
cc9e46e3-d437-4d19-a4fe-3245f5e840be 2019-01-17 22:56:27.432
672d1115-10e0-40b8-9ca7-40b0d587c85a 2019-01-17 22:56:09.961
7eb5afd7-dc48-4bb2-bb51-cf33dfeafe3f 2019-01-17 22:52:42.589

                                             ...          

eb2bb272-2205-43b2-8984-927cfffbc2fd 2019-01-14 12:45:35.045
8bcee5a8-2244-4f1f-bbf4-f05cd40513ed 2019-01-14 12:38:43.214
b0d312fb-01d2-4398-bf3f-798aab7f914d 2019-01-14 12:38:14.694
d7ce9ed6-db9d-4c0c-8256-7d94e135341f 2019-01-14 12:11:20.465
cfbda676-331c-4cb8-afbb-ad195a035d13 2019-01-14 12:10:21.011
3fd28c37-44d3-4031-8507-a15b1bbbb1d1 2019-01-14 11:41:04.615
9fd55a9d-33ff-47cd-888b-01bc0b5e1793 2019-01-14 10:50:34.951
bc84a61f-a368-4119-9522-6c6bd5fd4408 2019-01-14 10:38:12.292
6a69d9e8-2a49-4762-abd1-c6ce79f48986 2019-01-14 10:31:24.628
ab043246-5ea3-41ec-9a7b-1db7b7b6c863 2019-01-14 09:29:04.368
6bbe661f-da05-4811-b719-d7a14ba262e5 2019-01-14 06:55:29.992
e5c4bfa3-a052-4c8c-929b-8f5000a24acd 2019-01-14 06:51:05.613
9a116376-5340-42f4-9bce-6d9d05ea7bee 2019-01-14 06:42:46.614
5b971ccf-0f55-4244-a433-49e590e7d5de 2019-01-14 06:37:49.787
fd10da87-2187-4804-9e4d-a6a7d1cef375 2019-01-14 06:24:42.298
b9faaa07-ec43-4add-b0f7-b5a40968d573 2019-01-14 04:32:17.795
688b367b-e9f7-413d-9e60-4e1b73a138f2 2019-01-14 04:06:16.833
fcf902c7-dba9-4b51-89bb-36668ec447d2 2019-01-14 02:50:02.308
bedfd669-5f2f-44c2-b26c-32a3835196c8 2019-01-14 02:48:28.684
ea3c6ed4-f98b-4032-8cf8-df78bf04c4f7 2019-01-14 02:47:13.542
1817d944-e71a-4f74-95b8-6ceb5e529ca8 2019-01-14 02:46:02.994
732c0c33-5824-4876-a530-fce7911e47fe 2019-01-14 02:44:04.990
f54efa6c-2379-4d3d-a8ec-6388db46b8b7 2019-01-14 02:42:27.867
c348cc27-4906-4fd0-980f-fc96dcf0115f 2019-01-14 02:40:45.497
5d11ae5c-1d7a-41e8-9963-dd051f091ad9 2019-01-14 02:39:25.439
f692de03-389e-4ca8-bc19-fc13bff379df 2019-01-14 02:38:06.084
44f79ab5-aca4-480a-b5f7-24df5ea887ba 2019-01-14 02:36:24.071
98f64b91-6192-4ae3-8d26-d65e50835a26 2019-01-14 02:15:56.977
b581804c-ef22-4b18-90a4-a4df260c91c5 2019-01-14 02:06:31.885
93f57887-7840-4b08-be9e-7e239f6eb569 2019-01-14 02:03:37.644
Name: Time, Length: 367, dtype: datetime64[ns]
我想绘制这些时间的直方图。但是,如果我尝试

df.hist(column='Time')
我收到以下错误消息:


ValueError Traceback (most recent call last)
in
----> 1 exec(open('analysis/check_refunds_fix.py').read())

in

/usr/local/lib/python3.7/site-packages/pandas/plotting/_core.py in hist_frame(data, column, by, grid, xlabelsize, xrot, ylabelsize, yrot, ax, sharex, sharey, figsize, layout, bins, **kwds)
2406 fig, axes = _subplots(naxes=naxes, ax=ax, squeeze=False,
2407 sharex=sharex, sharey=sharey, figsize=figsize,
-> 2408 layout=layout)
2409 _axes = _flatten(axes)
2410

/usr/local/lib/python3.7/site-packages/pandas/plotting/_tools.py in _subplots(naxes, sharex, sharey, squeeze, subplot_kw, ax, layout, layout_type, **fig_kw)

236 
237     # Create first subplot separately, so we can share it if requested

--> 238 ax0 = fig.add_subplot(nrows, ncols, 1, **subplot_kw)

239 
240     if sharex:

/usr/local/lib/python3.7/site-packages/matplotlib/figure.py in add_subplot(self, args, *kwargs)
1365 self._axstack.remove(ax)
1366
-> 1367 a = subplot_class_factory(projection_class)(self, args, *kwargs)
1368 self._axstack.add(key, a)
1369 self.sca(a)

/usr/local/lib/python3.7/site-packages/matplotlib/axes/_subplots.py in __init__(self, fig, args, *kwargs)

 58                     raise ValueError(
 59                         ("num must be 1 <= num <= {maxn}, not {num}"

---> 60 ).format(maxn=rows*cols, num=num))

 61                 self._subplotspec = GridSpec(
 62                         rows, cols, figure=self.figure)[int(num) - 1]

ValueError: num must be 1 <= num <= 0, not 1
错误消息对我来说没有意义,因为没有数字num可以满足1 <= num <= 0。我试图用一个“简单”的例子重现这个,但我得到了直方图。
更新

我matplotlib在SubplotBase类的源代码中设置了跟踪:

class SubplotBase(object):

"""
Base class for subplots, which are :class:`Axes` instances with
additional methods to facilitate generating and manipulating a set
of :class:`Axes` within a figure.
"""

def __init__(self, fig, *args, **kwargs):
    """
    *fig* is a :class:`matplotlib.figure.Figure` instance.

    *args* is the tuple (*numRows*, *numCols*, *plotNum*), where
    the array of subplots in the figure has dimensions *numRows*,
    *numCols*, and where *plotNum* is the number of the subplot
    being created.  *plotNum* starts at 1 in the upper left
    corner and increases to the right.

    If *numRows* <= *numCols* <= *plotNum* < 10, *args* can be the
    decimal integer *numRows* * 100 + *numCols* * 10 + *plotNum*.
    """

    self.figure = fig

    if len(args) == 1:
        if isinstance(args[0], SubplotSpec):
            self._subplotspec = args[0]
        else:
            try:
                s = str(int(args[0]))
                rows, cols, num = map(int, s)
            except ValueError:
                raise ValueError('Single argument to subplot must be '
                    'a 3-digit integer')
            self._subplotspec = GridSpec(rows, cols,
                                         figure=self.figure)[num - 1]
            # num - 1 for converting from MATLAB to python indexing
    elif len(args) == 3:
        rows, cols, num = args
        rows = int(rows)
        cols = int(cols)
        if isinstance(num, tuple) and len(num) == 2:
            num = [int(n) for n in num]
            self._subplotspec = GridSpec(
                    rows, cols,
                    figure=self.figure)[(num[0] - 1):num[1]]
        else:
            if num < 1 or num > rows*cols:
                raise ValueError(
                    ("num must be 1 <= num <= {maxn}, not {num}"
                    ).format(maxn=rows*cols, num=num))
            self._subplotspec = GridSpec(
                    rows, cols, figure=self.figure)[int(num) - 1]
            # num - 1 for converting from MATLAB to python indexing
    else:
        raise ValueError('Illegal argument(s) to subplot: %s' % (args,))

在elif len(args) == 3block,我可以证实,构造函数调用rows = 1,cols = 0和num = 1。我猜问题是这样的cols = 0; 为什么它试图初始化没有任何列的子图?

Python
分享到
取消 提交回答
全部回答(1)
  • 一码平川MACHEL
    2019-07-17 23:26:05

    我设法通过调用来解决该问题hist()的方法pd.Series,df['Time']:

    In [5]: hist = df['Time'].hist()

    In [6]: plt.show()
    这绘制了直方图:
    KXYcd

    0 0

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题