What is wrong with the data that I am trying to send to matplotlib?

I have the following script:

import pandas
from collections import Counter
import matplotlib.pyplot as plt

while True:
    data = [int(x) for x in raw_input("Enter the list containing the data: ").split()]
    letter_counts = Counter(data)
    df = pandas.DataFrame.from_dict(letter_counts, orient="index")
    df.plot(kind="bar")
    plt.show()

When I either type or copy and paste a series or numbers, for instance,

 1 4 5 6 3

the script works perfectly and shows me the histogram. However, when I paste numbers from the output I get from a different terminal window, for instance:

  13 13 16 16 16 16 9 9 9 9 9 15 15 15 15 20 20 20 20 20 22 22 22 22 13
  13 13 13 12 12 12 12 12 16 16 16 16 15 15 15 15 15 15 15 15 15 15 15
  15 15 22 22 22 22 22 15 15 15 15 13 13 13 13 13 18 18 18 18 10 10 10
  10 12 12 12 12 12 10 10 10 10 20 20 20 20 20 15 15 15 15 15 15 15 15
  17 17 17 17 17 13

The first time I enter the data, it works perfectly; however, when I enter it the second time, it doesn't do anything and then I have to hit enter again. It shows me the plot, but when I close it, it gives me the following error:

> Enter the list containing the data: Traceback (most recent call last):
> File "make_histo.py", line 9, in <module>
>     df.plot(kind="bar")   File "/usr/local/lib/python2.7/dist-packages/pandas/plotting/_core.py",
> line 2627, in __call__
>     sort_columns=sort_columns, **kwds)   File "/usr/local/lib/python2.7/dist-packages/pandas/plotting/_core.py",
> line 1869, in plot_frame
>     **kwds)   File "/usr/local/lib/python2.7/dist-packages/pandas/plotting/_core.py",
> line 1694, in _plot
>     plot_obj.generate()   File "/usr/local/lib/python2.7/dist-packages/pandas/plotting/_core.py",
> line 243, in generate
>     self._compute_plot_data()   File "/usr/local/lib/python2.7/dist-packages/pandas/plotting/_core.py",
> line 352, in _compute_plot_data
>     'plot'.format(numeric_data.__class__.__name__)) 
TypeError: Empty 'DataFrame': no numeric data to plot

What am I doing wrong?

Upvotes: 0

Views: 82

Answers (1)

AS Mackay
AS Mackay

Reputation: 2857

I don't quite get the behavior you described: when I copy-paste the block of numbers from your question I get embedded line breaks and this causes raw_input() to get called multiple times.

A possible workaround for that problem is to make the program treat an empty line as end-of-input: the following very simple code accepts a copy-paste of your block of numbers OK on my system (Windows, Python 2.7):

while True:
    print ("Enter the list containing the data: ")
    lines = []
    while True:
        line = raw_input()
        if (line):
            lines.append(line.lstrip().strip())
        else:
            break
    data = []
    for line in lines:
        for x in line.split():
            data.append(int(x))
    print data 

Hope this may be helpful.

Upvotes: 1

Related Questions