0

I´m trying to add some text for a note in a matplotlib plot, and i want to get that text from a QTextEdit, which is in a QDialog that pops-up with a button in the toolbar.

This is my code so far:

class TextoMatplotlib(QDialog):
    def __init__(self):
      QDialog.__init__(self)
      uic.loadUi("insertartextoenMatplotlib.ui", self) # Created in QtDesigner
      #Matplotlib figure
      fig = plt.figure(1, figsize=(5,5))
      fig.clf()
      ax = fig.add_subplot(111)
      ax.plot()
      self.connect(self.textEdit, SIGNAL("returnPressed()"),self.InsertText) 

Here is the method that i want to connect with the QTextEdit (it´s all in the same class):

     def InsertText(self):
       nota.insertPlainText(self.textEdit.text())
       nota.setText('')

       notes = ax.annotate(nota, xy=(), bbox = dict(facecolor="red"))
       listadenotas = []
       for note in notes:
          nota = DraggableNote(note)
          nota.connect()
          listadenotas.append(nota)

class DraggableNote:
    def __init__(self, note):
      self.note = nota
      self.press = None

    def connect(self):
      self.cidpress = self.note.figure.canvas.mpl_connect(
          "button_pressed_event", self.on_press)
      self.cidrelease = self.note.figure.canvas.mpl_connect(
          "button_release_event", self.on_release)
      self.cidmotion = self.note.figure.canvas.mpl_connect(
          "motion_notify_event", self.on_motion)

    def on_press(self, event):
      if event.inaxes != self.note.axes:
          return
      contains, attrd = self.note.contains(event)

      if not contains:
          return
          x0, y0 = self.note.xy
          self.press = x0, y0, event.xdata, event.ydata

    def on_motion(self, event):

      if self.press is None:
          return
      if event.inaxes != self.note.axes:
          return
      x0, y0, xpress, ypress = self.press
      dx = event.xdata - xpress
      dy = event.ydata - ypress

      self.note.set_x(x0+dx)
      self.note.set_y(y0+dy)

      self.note.figure.canvas.draw()

  def on_release(self, event):

      self.press = None
      self.note.figure.canvas.draw()

  def disconnect(self):
      self.note.figure.canvas.mpl_disconnect(self.cidpress)
      self.note.figure.canvas.mpl_disconnect(self.cidrelease)
      self.note.figure.canvas.mpl_disconnect(self.cidmotion)

I´ve got the "DraggableNote" class from an example in the Matplotlib documentation.

I don´t know where the problem is. I may have copy/paste it wrong in here, but the indentation is correct. Sorry if there is a mistake of that kind.

I hope you can help me. Thank you for your answers.

------------------------------ EDIT------------------------------------------

I tried to make it more easier. This is what i´ve done:

class TextoMatplotlib(QDialog):
    def __init__(self):
      QDialog.__init__(self)
      uic.loadUi("insertartextoenMatplotlib2.ui", self) 

      self.setWindowTitle("Insertar Nota")
      self.setMinimumSize(250, 120)
      self.setMaximumSize(250, 120)
      self.label.setText("Insertar texto: ")

      x = np.arange(0, 5, 0.1);
      y = np.sin(x)

      fig, ax = plt.subplots()
      ax.plot(x,y)

      nota = self.textEdit.toPlainText()
      self.connect(self.textEdit, SIGNAL("returnPressed()"), self.insertText)

    def insertText(self):
      note = ax.annotate(nota, xy=(2, 0), bbox=dict(facecolor='yellow'))  
      note.draggable()

It still does not work. I really don´t know what i´m missing here

Pablo Flores
  • 667
  • 1
  • 13
  • 33
  • There are a lot of errors in this code (typos), so it wouldn't even get to the point of putting the text into the matplotlib plot. What error are you getting, and is this the original code or has it been modified? – Score_Under Jan 18 '16 at 21:12
  • Thank you, i already corrected an error of typo ("listadenotas"). With this code nothing happens. A plot and the Qdialog are shown, but they are not connect at all. I mean that the note does not appears. – Pablo Flores Jan 18 '16 at 21:38
  • There is another typo left in the DraggableNote class: you say `self.note = nota`, but you probably meant to use `note` (in the `__init__` param list). You are also using `nota` before it is assigned in `InsertText`. I don't know what the real problem with the code is (I suspect it's obscured by these problems), but I do notice that you're calling `setText('')` immediately after adding text, which would have the effect of undoing the text insertion. If you're running the code and it doesn't show any errors despite those typos, hunt for `try`/`except` blocks which might be eating the error. – Score_Under Jan 18 '16 at 21:46
  • I think you are just missing a call to `canvas.draw_idle()` – tacaswell Jan 18 '16 at 22:17
  • How do you run the code? If you are in an interactive session, chances are that you do not see exceptions thrown by Matplotlib's callbacks. If that is the case, run it non-interactively (e.g. from the command line with `python yourscript.py`). – MB-F Jan 19 '16 at 08:23
  • I always run it in the command prompt. I will try in another ways. If i reach what i want to, i will put it here. – Pablo Flores Jan 19 '16 at 20:14

0 Answers0