本文介绍了 pandas 直方图绘图错误:ValueError:num必须为1< = num< = 0,而不是1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从熊猫数据框中绘制列的直方图:

I am drawing a histogram of a column from pandas data frame:

%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

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

Does anyone know what this error mean? Thanks!

推荐答案

问题

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

Problem

The problem you encounter arises when column_A does not contain numeric data. As you can see in the excerpt from pandas.plotting._core below, the numeric data is essential to make the function hist_frame (which you call by DataFrame.hist()) work correctly.

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. 如果您的问题是用直方图表示数字数据(但不是数字dtype ),则需要使用pd.to_numericdf.astype(a_selected_numeric_dtype)将数据转换为数字,例如'float64',然后继续执行您的代码.

  1. If your problem is to represent numeric data (but not of numeric dtype yet) with a histogram, you need to cast your data to numeric, either with pd.to_numeric or df.astype(a_selected_numeric_dtype), e.g. 'float64', and then proceed with your code.

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

If your problem is to represent non-numeric data in one column with a histogram, you can call the function hist_series with the following line: df['column_A'].hist(bins=100).

如果您的问题是要用直方图表示许多列中的非数字数据,则可以采用一些选择:

If your problem is to represent non-numeric data in many columns with a histogram, you may resort to a handful options:

  • 使用matplotlib并直接创建子图和直方图
  • 将熊猫至少更新到版本0.25
  • Use matplotlib and create subplots and histograms directly
  • Update pandas at least to version 0.25

这篇关于 pandas 直方图绘图错误:ValueError:num必须为1&lt; = num&lt; = 0,而不是1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:24