0

So I'm trying to write a small GUI that will allow an end use to plot X vs Y of any 2 columns in an Excel file. Here's my code:

import pandas as pd
import matplotlib.pyplot as plt
import tkinter as tk

my_base=pd.read_excel('my_base.xlsx', 'the_tab', index_col=None, na_values = ['NA'])
my_base_header = list(my_base.columns.values)

my_base['Generated Date'] = pd.to_datetime(my_base['Generated Date'])

main_win = tk.Tk()

def plot_graph():
    print(option1.get())
    print(option2.get())
    my_base.plot(x = option1.get(), y = option2.get(), style = 'x')
    plt.show()

option1 = tk.StringVar(main_win)
option1.set(my_base_header[0])
option2 = tk.StringVar(main_win)
option2.set(my_base_header[0])

opt1 = tk.OptionMenu(main_win, option1, *my_base_header)
opt1.pack()
opt2 = tk.OptionMenu(main_win, option2, *my_base_header)
opt2.pack()

runbtn = tk.Button(main_win, text = 'Plot', command = plot_graph)
runbtn.pack()

main_win.mainloop()

I can get the program to plot if I put the dataframe headers in directly like so:

my_base.plot(x = 'Generated Date', y = 'How many', style = 'x')

But when I use for example x = option1.get() in there I get this traceback

Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    my_base.plot(x= x_ax, y = y_ax, style = 'x')
  File "C:\Python34\lib\site-packages\pandas\tools\plotting.py", line 2485, in plot_frame
    **kwds)
  File "C:\Python34\lib\site-packages\pandas\tools\plotting.py", line 2325, in _plot
    plot_obj.generate()
  File "C:\Python34\lib\site-packages\pandas\tools\plotting.py", line 921, in generate
    self._compute_plot_data()
  File "C:\Python34\lib\site-packages\pandas\tools\plotting.py", line 997, in _compute_plot_data
    'plot'.format(numeric_data.__class__.__name__))
TypeError: Empty 'Series': no numeric data to plot
1
  • this doesn't look related to Tkinter. You need to make your example reproducible. In this case, skip reading the Excel file and generate some random data to see if the problem persists. Commented Apr 14, 2015 at 1:39

1 Answer 1

1

It's as the error says: the data you're trying to plot is non-numeric, so it's probably a string or from the looks of it, maybe a datetime. If you include the data and which column is giving you this error, we could point out the problem.

If it is a datetime, you may need to convert it to a pandas timestamp type, like:

pandas.DatetimeIndex([yourDatetime])

Your code seems to work for me. I used this demo dataset: http://www.contextures.com/xlSampleData01.html

If I try to plot something non-numeric, such as 'Region' or 'Rep', it gives me the same error you have (no numeric data to plot). If I plot 'Unit Cost' vs 'Total', or any other combination of 2 numeric datasets, it works.

import pandas as pd
import matplotlib.pyplot as plt
import Tkinter as tk

my_base=pd.read_excel('SampleData.xls', 'SalesOrders', index_col=None, na_values = ['NA'])
my_base_header = list(my_base.columns.values)

#print my_base
my_base['OrderDate'] = pd.to_datetime(my_base['OrderDate'])

main_win = tk.Tk()

def plot_graph():
    print(option1.get())
    print(option2.get())
    my_base.plot(x = option1.get(), y = option2.get(), style = 'x')
    plt.show()

option1 = tk.StringVar(main_win)
option1.set(my_base_header[0])
option2 = tk.StringVar(main_win)
option2.set(my_base_header[0])

opt1 = tk.OptionMenu(main_win, option1, *my_base_header)
opt1.pack()
opt2 = tk.OptionMenu(main_win, option2, *my_base_header)
opt2.pack()

runbtn = tk.Button(main_win, text = 'Plot', command = plot_graph)
runbtn.pack()

main_win.mainloop()
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, so that was basically the problem. I had scrapped several thousand PDFs previously, and apparently a few of the scrapped PDFs turned up junk (strings in place of integers) in some of the columns. Pyplot of course didn't like this when it tried to plot it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.