Pandas dataframe: ValueError: num must be 1 <= num <= 0, not 1
<p>I am getting the following error while I am trying to plot a <code>pandas dataframe</code>:</p>

<blockquote>
  <p>ValueError: num must be 1 &lt;= num &lt;= 0, not 1</p>
</blockquote>

<p>Code:</p>

<pre><code>import matplotlib.pyplot as plt

names = ['buying', 'maint', 'doors', 'persons', 'lug_boot', 'safety']
custom = pd.DataFrame(x_train)  //only a portion of the csv
custom.columns = names
custom.hist()
plt.show()
</code></pre>

<p>I have tried to read the file again from the <code>csv</code> and I am getting the exact same error.</p>

<p>Edit:</p>

<p><code>print x_train</code> output:</p>

<blockquote>
  <p>[[0.0 0.0 0.0 0.0 0.0 0.0]</p>
  
  <p>[1.0 1.0 0.0 0.0 0.0 0.0]</p>
  
  <p>[0.0 0.0 0.0 0.0 0.0 0.0]</p>
  
  <p>..., </p>
  
  <p>[0.0 0.0 0.0 0.0 0.0 0.0]</p>
  
  <p>[0.3333333333333333 0.3333333333333333 2.0 2.0 2.0 2.0]</p>
  
  <p>[0.0 0.0 3.0 3.0 3.0 3.0]]</p>
</blockquote>

<p>Edit2:</p>

<p>Complete list of errors(Traceback): </p>

<blockquote>
  <p>Traceback (most recent call last):</p>
  
  <p>File "temp.py", line 104, in 
      custom.dropna().hist()</p>
  
  <p>File "/home/kostas/anaconda2/lib/python2.7/site-packages/pandas/tools/plotting.py", line 2893, in hist_frame
      layout=layout)</p>
  
  <p>File "/home/kostas/anaconda2/lib/python2.7/site-packages/pandas/tools/plotting.py", line 3380, in _subplots
      ax0 = fig.add_subplot(nrows, ncols, 1, **subplot_kw)</p>
  
  <p>File "/home/kostas/anaconda2/lib/python2.7/site-packages/matplotlib/figure.py", line 1005, in add_subplot
      a = subplot_class_factory(projection_class)(self, *args, **kwargs)</p>
  
  <p>File "/home/kostas/anaconda2/lib/python2.7/site-packages/matplotlib/axes/_subplots.py", line 64, in <strong>init</strong>
      maxn=rows*cols, num=num))</p>
</blockquote>

<p>So I'm pretty sure your issue is something to do with the format of the array train_x. I tried your program with an array of 10,000 rows and 6 cols and it worked fine so the issue is not size. For some reason, one of <code>len(x_train)</code> or <code>len(x_train[0])</code> is 0. What makes me think this is thus:</p>

<p>The ValueError you are getting is from the matplotlib.axes._subplot module which deals with drawing many small subplots within a big plot (so each small histogram). The code of the module is this:</p>

<pre><code>""" 
*rows*, *cols*, *num* are arguments where
the array of subplots in the figure has dimensions *rows*,
*cols*, and where *num* is the number of the subplot
being created. *num* starts at 1 in the upper left
corner and increases to the right.
"""
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)[num[0] - 1:num[1]]
else:
    if num &lt; 1 or num &gt; rows*cols:
        raise ValueError(      
            "num must be 1 &lt;= num &lt;= {maxn}, not {num}".format(
                maxn=rows*cols, num=num))
</code></pre>

<p>Your issue is in this part (see explanation in comments in code): </p>

<pre><code>    if num &lt; 1 or num &gt; rows*cols:
     # maxN is the number of rows*cols and since this is showing 0 for you (in your error stacktrace), 
     # it means the number of cols being passed into your histogram is 0. Don't know why though :P
        raise ValueError(      
            "num must be 1 &lt;= num &lt;= {maxn}, not {num}".format(
                maxn=rows*cols, num=num))
</code></pre>

<p>I don't know how you are reading your input format, but I'm pretty sure the problem is related to it. If you set x_train to this it works fine:</p>

<pre><code>    x_train =   [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],

                [1.0, 1.0, 0.0, 0.0, 0.0, 0.0],

                [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],

                [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],

                [0.3333333333333333, 0.3333333333333333, 2.0, 2.0, 2.0, 2.0],

                [0.0, 0.0, 3.0, 3.0, 3.0, 3.0]]
</code></pre>

<p>Try doing this before calling the code in your question and see if that works:</p>

<pre><code>x_train = list([list(x) for x in x_train])
</code></pre>

python pandas histogram