【问题标题】:pandas histogram plot error: ValueError: num must be 1 <= num <= 0, not 1pandas histogram plot error: ValueError: num must be 1 <= num <= 0, not 1
【发布时间】:2017-01-27 15:16:25
【问题描述】:

我正在从 pandas 数据框中绘制一个列的直方图:

%matplotlib notebook
import matplotlib.pyplot as plt
import matplotlib
df.hist(column='column_A', bins = 100)

但出现以下错误:

     62                     raise ValueError(
     63                         "num must be 1 <= num <= {maxn}, not {num}".format(
---> 64                             maxn=rows*cols, num=num))
     65                 self._subplotspec = GridSpec(rows, cols)[int(num) - 1]
     66                 # num - 1 for converting from MATLAB to python indexing

ValueError: num must be 1 <= num <= 0, not 1

有谁知道这个错误是什么意思?谢谢!

【问题讨论】:

  • column_A 的数据类型是什么 - 它是数字数据类型吗?
  • @MaxU:你是对的。我忘了转换类型。感谢您的提醒。现在工作
  • 你能更清楚地告诉我你是如何解决你的问题的吗?我有同样的问题:yearly_average[:10].hist(column='year')

标签: pandas dataframe histogram


【解决方案1】:

问题

column_A 不包含数字数据时,就会出现您遇到的问题。正如您在下面的 pandas.plotting._core 的摘录中看到的那样,数字数据对于使函数 hist_frame(您调用 DataFrame.hist())正常工作至关重要。

def hist_frame(data, column=None, by=None, grid=True, xlabelsize=None,
               xrot=None, ylabelsize=None, yrot=None, ax=None, sharex=False,
               sharey=False, figsize=None, layout=None, bins=10, **kwds):
    # skipping part of the code
    # ...
    if column is not None:
        if not isinstance(column, (list, np.ndarray, Index)):
            column = [column]
        data = data[column]
    data = data._get_numeric_data()  # there is no numeric data in the column
    naxes = len(data.columns)  # so the number of axes becomes 0
    # naxes is passed to the subplot generating function as 0 and later determines the number of columns as 0
    fig, axes = _subplots(naxes=naxes, ax=ax, squeeze=False,
                          sharex=sharex, sharey=sharey, figsize=figsize,
                          layout=layout)
    # skipping the rest of the code
    # ...

解决方案

  1. 如果您的问题是用直方图表示 numeric 数据(但不是 numeric dtype yet),您需要使用pd.to_numericdf.astype(a_selected_numeric_dtype) 将您的数据转换为数字,例如'float64',然后继续执行您的代码。

  2. 如果您的问题是用直方图表示一列中的非数字数据,您可以使用以下行调用函数hist_seriesdf['column_A'].hist(bins=100)

  3. 如果您的问题是用直方图表示 许多列 中的 非数字 数据,您可能会求助于几个选项:

    • 使用matplotlib 并直接创建子图和直方图
    • 至少将 pandas 更新到版本 0.25

【讨论】:

    【解决方案2】:

    通常为 0

    mta['penn'] = [mta_bystation[mta_bystation.STATION == "34 ST-PENN STA"], 'Penn Station']
    mta['grdcntrl'] = [mta_bystation[mta_bystation.STATION == "GRD CNTRL-42 ST"], 'Grand Central']
    mta['heraldsq'] = [mta_bystation[mta_bystation.STATION == "34 ST-HERALD SQ"], 'Herald Sq']
    mta['23rd'] = [mta_bystation[mta_bystation.STATION == "23 ST"], '23rd St']
    #mta['portauth'] = [mta_bystation[mta_bystation.STATION == "42 ST-PORT AUTH"], 'Port Auth']
    #mta['unionsq'] = [mta_bystation[mta_bystation.STATION == "14 ST-UNION SQ"], 'Union Sq']
    mta['timessq'] = [mta_bystation[mta_bystation.STATION == "TIMES SQ-42 ST"], 'Ti
    

    【讨论】:

    • 您愿意详细说明吗?按照目前的情况,您的回答毫无意义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    • 2022-07-16
    相关资源
    最近更新 更多