python - Can't add legend in figure -


i want add legend figure, doesn't show. in code below "forecast_canvas" instance of class canvas created in qt designer.

    ax = self.forecast_canvas.figure.add_subplot(111)     ax.plot(self.new,'--',label='observed')      ax.plot(data,color='green',label='forecast')     self.forecast_canvas.draw() 

here's code of canvas :

from matplotlib.backends.backend_qt4agg import figurecanvasqtagg figurecanvas   import matplotlib.pyplot plt pyqt4 import qtgui   class   canvas(figurecanvas):       def __init__(self, parent=none):          self.figure = plt.figure()     #plt.tight_layout(pad=4)          figurecanvas.__init__(self, self.figure)          self.setparent(parent)          figurecanvas.setsizepolicy(self,                                 qtgui.qsizepolicy.expanding,                                 qtgui.qsizepolicy.expanding)          figurecanvas.updategeometry(self)          #plt.legend() 

i tried adding

plt.legend()

in canvas class didn't work neither. enter image description here

i think have 2 issues here:

  1. you need add legend after creating plots.
  2. you may not able use pyplot (plt.legend()) because pyplot may not host figures. (i'm saying "may" here, because don't know full code)
    in case it's safer draw legend particular axes; in case use

    ax.legend() 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -