我正在从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
有谁知道这个错误是什么意思?谢谢!
答案 0 :(得分:0)
当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
# ...
如果您的问题是用直方图表示数字数据(但不是数字dtype ),则需要使用pd.to_numeric
或df.astype(a_selected_numeric_dtype)
将数据转换为数字,例如'float64'
,然后继续执行您的代码。
如果您的问题是用直方图表示一列中的非数字数据,则可以使用以下命令调用函数hist_series
行:df['column_A'].hist(bins=100)
。
如果您的问题是用直方图表示许多列中的非数字数据,则可以采用以下几种选择:
matplotlib
并直接创建子图和直方图0.25
答案 1 :(得分:-1)
通常为 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