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
tacaswell
  • 84,579
  • 22
  • 210
  • 199
user2236076
  • 154
  • 1
  • 2
  • 8
  • 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? – BrenBarn May 02 '13 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 – cantdutchthis May 02 '13 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. – tacaswell May 02 '13 at 19:20
  • 1
    You should use one of the non-interactive backends (as it seems you don't have a X server running) – tacaswell May 02 '13 at 19:33

1 Answers1

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.

Community
  • 1
  • 1
tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • 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 May 02 '13 at 19:22
  • 2
    @user2236076: That sounds like your backend isn't set up right. You might want to ask a separate question about that. – BrenBarn May 02 '13 at 19:23