By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,362 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,362 IT Pros & Developers. It's quick & easy.

Need help displaying text underneath bar on an tkinter embedded matpotlib bar graph

P: 11
I was successful on embedding a matpotlib bar graph onto tkinter, however I can seem to find a way to disply text underneat each bar. The desired result would be text displaying the content of each bar underneath, for example
bar 1, bar 2, bar 3, bar 4, bar 5, any help would be greatly apperciated.

Happy holidays to all the programming community!

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python
  2. import matplotlib
  3. matplotlib.use('TkAgg')
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. from numpy import arange, sin, pi
  7. from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
  8. from matplotlib.figure import Figure
  9. from Tkinter import Tk, Frame, Menu
  10. import sys
  11. if sys.version_info[0] < 3:
  12.     import Tkinter as Tk
  13. else:
  14.     import tkinter as Tk
  15.  
  16. def sabina():
  17.     print "Sabina"
  18.  
  19. def saoPaulo():
  20.     print "Sao Paulo"
  21.  
  22. def NSMX():
  23.     print "NSMX"
  24. def knob():
  25.     print "KNOB"
  26. def pointer():
  27.     print "POINTER"
  28. def ambient():
  29.     print "AMBIENT"
  30.  
  31. class Example(Frame):
  32.  
  33.     def __init__(self, parent):
  34.         Frame.__init__(self, parent)   
  35.  
  36.         self.parent = parent
  37.  
  38.         self.initUI()
  39.  
  40.     def initUI(self):
  41.  
  42.         self.parent.title("Submenu")
  43.  
  44.         menubar = Menu(self.parent)
  45.         self.parent.config(menu=menubar)
  46.  
  47.         customers = Menu(menubar)       
  48.         customers.add_command(label='Sabina', underline=0, command=sabina)
  49.         customers.add_command(label='Sao Paulo', underline=0, command=saoPaulo)
  50.         customers.add_command(label='NSMX', underline=0, command=NSMX)
  51.         customers.add('separator')        
  52.         customers.add_command(label="Exit", underline=0, command=self.onExit)
  53.  
  54.  
  55.  
  56.         menuProducts = Menu(menubar)       
  57.         menuProducts.add_command(label='Knob', underline=0, command=knob)
  58.         menuProducts.add_command(label='Pointer', underline=0, command=pointer)
  59.         menuProducts.add_command(label='Ambient', underline=0, command=ambient)
  60.         menuProducts.add('separator')        
  61.         menuProducts.add_command(label="Exit", underline=0, command=self.onExit)      
  62.  
  63.         menuMaq = Menu(menubar)       
  64.         menuMaq.add_command(label='Sabina', underline=0, command=sabina)
  65.         menuMaq.add_command(label='Sao Paulo', underline=0, command=saoPaulo)
  66.         menuMaq.add_command(label='NSMX', underline=0, command=NSMX)
  67.         menuMaq.add('separator')        
  68.         menuMaq.add_command(label="Exit", underline=0, command=self.onExit)
  69.  
  70.         menubar.add_cascade(label="Customers", underline=0, menu=customers)        
  71.         menubar.add_cascade(label="Products", underline=0, menu=menuProducts)                  
  72.  
  73.     def onExit(self):
  74.         self.quit()
  75.  
  76.  
  77. def destroy(e): sys.exit()
  78.  
  79. root = Tk.Tk()
  80. root.wm_title("Produccion Status")
  81. #root.bind("<Destroy>", destroy)
  82. prodName='Model name'
  83. pO=19000
  84. po=[19000,19000,19000,19000,19000]
  85. N = ['Cap', 'bush', 'pointer', 'omori', 'KanseiHin']
  86. existencias   = [10000, 2000, 1100, 3500,3000]
  87. retrasos = [int(pO-existencias[0]),int(pO-existencias[1]),\
  88.             int(pO-existencias[2]),int(pO-existencias[3]),int(pO-existencias[4])]#[5000, 8200, 9400, 2000]
  89. ind = np.arange(len(N))    # the x locations for the groups
  90. width = .80      # the width of the bars: can also be len(x) sequence
  91.  
  92. f = Figure(figsize=(3,3), dpi=150)#window size
  93. a = f.add_subplot(111)#layout
  94. #t = arange(00,10,001)#xticks
  95. #s = sin(2*pi*t)
  96. a.bar(ind, po,   width, color='r')
  97. a.bar(ind, existencias,   width, color='y')
  98. #a.bar(ind, retrasos,   width, color='r')
  99. a.set_title(prodName)
  100. #ylabel('Cantidades')
  101. #title('Nombre del Modelo')
  102. a.set_xticks(ind+width/2., ('Cap', 'bush', 'pointer', 'omori', 'KanseiHin') )
  103. a.set_yticks(np.arange(0,20000,1000))
  104. a.set_xlabel('buhin')
  105. a.set_ylabel('Cantidades')
  106.  
  107.  
  108. # a tk.DrawingArea
  109. canvas = FigureCanvasTkAgg(f, master=root)
  110. canvas.show()
  111. #canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
  112.  
  113. #toolbar = NavigationToolbar2TkAgg( canvas, root )
  114. #toolbar.update()
  115. canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
  116.  
  117. button = Tk.Button(master=root, text='Quit', command=sys.exit)
  118. button.pack(side=Tk.BOTTOM)
  119. app = Example(root)
  120. app1 = Example(root)
  121. Tk.mainloop()
  122.  
Dec 19 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.