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
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
# ...
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.
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
and create subplots and histograms directly0.25