3

I am struggling with this really badly. There is something that I'm just not getting. I have a function, which I want to plot a histogram of a dictionary with the keys on the x-axis and the values on the y-axis, then save the file in a location specified when calling the function. What I have is:

​import matplotlib.pyplot as plt


def test(filename):
    dictionary = {0:1000, 1:20, 2:15, 3:0, 4:5}
    xmax = max(dictionary.keys())
    ymax = max(dictionary.values())
    plt.hist(dictionary,xmax)
    plt.title('Histogram Title')
    plt.xlabel('Label')
    plt.ylabel('Another Label')
    plt.axis([0, xmax, 0, ymax])
    plt.figure()
    plt.savefig(filename)

test('test_graph.svg')

I simply cannot get this to work, and I've struggled for a very long time reading other questions and documentation. Any help would be greatly appreciated. Thanks.

EDIT:

The error I have is:

File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 343, in figure
    **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 80, in new_figure_manager
    window = Tk.Tk()
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1688, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
TclError: no display name and no $DISPLAY environment variable
4
  • You need to explain what "does not work" means. What is it doing that you consider "not working"? If you get an error, what is the error message? Commented May 2, 2013 at 19:09
  • usually you would get an error message when you try to run such a program. what is the error message? if you add this then we might be able to help Commented May 2, 2013 at 19:11
  • @BrenBarn This is an issue with usage the state-machine interface of matplotlib. It will silently do the 'wrong' thing because this is perfectly valid and correct (but not functional) code. Commented May 2, 2013 at 19:20
  • 1
    You should use one of the non-interactive backends (as it seems you don't have a X server running) Commented May 2, 2013 at 19:33

1 Answer 1

3

You are getting snarled up by the state-machine interface:

import matplotlib.pyplot as plt

def test(filename):
    dictionary = {0:1000, 1:20, 2:15, 3:0, 4:5}
    xmax = max(dictionary.keys())
    ymax = max(dictionary.values())
    plt.figure() # <- makes a new figure and sets it active (add this)
    plt.hist(dictionary,xmax) # <- finds the current active axes/figure and plots to it
    plt.title('Histogram Title') 
    plt.xlabel('Label')
    plt.ylabel('Another Label')
    plt.axis([0, xmax, 0, ymax])
    # plt.figure() # <- makes new figure and makes it active (remove this)
    plt.savefig(filename) # <- saves the currently active figure (which is empty in your code)

test('test_graph.svg')

See How can I attach a pyplot function to a figure instance? for a longer explanation of the state-machine vs OO interfaces for matplotlib.

Sign up to request clarification or add additional context in comments.

2 Comments

I updated to your code but I'm still getting this error: File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 343, in figure **kwargs) File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 80, in new_figure_manager window = Tk.Tk() File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1688, in init self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use) TclError: no display name and no $DISPLAY environment variable
@user2236076: That sounds like your backend isn't set up right. You might want to ask a separate question about that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.