graph - Histogram in Python Using matplotlib -
this question has answer here:
i struggling badly. there i'm not getting. have function, want plot histogram of dictionary keys on x-axis , values on y-axis, save file in location specified when calling function. have is:
import matplotlib.pyplot 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 cannot work, , i've struggled long time reading other questions , documentation. appreciated. thanks.
edit:
the error 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 , no $display environment variable
you getting snarled state-machine interface:
import matplotlib.pyplot 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 new figure , sets active (add this) plt.hist(dictionary,xmax) # <- finds current active axes/figure , plots plt.title('histogram title') plt.xlabel('label') plt.ylabel('another label') plt.axis([0, xmax, 0, ymax]) # plt.figure() # <- makes new figure , makes active (remove this) plt.savefig(filename) # <- saves active figure (which empty in code) test('test_graph.svg')
see how can attach pyplot function figure instance? longer explanation of state-machine vs oo interfaces matplotlib.
Comments
Post a Comment