4

I am not sure if this is an acceptable question in SE. I am wondering if it is possible to edit matplotlib plot interactively. i.e.,

#  plot
plt.plot(x, y[1])
plt.plot(x, -1.0*y[2])
plt.show()

will open up a tk screen with the plot. Now, say, I want to modify the linewidth or enter x/y label. Is it possible to do that interactively (either on the screen, using mouse like xmgrace or from a gnuplot like command prompt)?

Cœur
  • 37,241
  • 25
  • 195
  • 267
BaRud
  • 3,055
  • 7
  • 41
  • 89

4 Answers4

6

You can do simple interactive editing with pylustrator

pip install pylustrator
Anton Kühl
  • 61
  • 1
  • 2
  • welcome to so. its a 4 yo queston, and resolved. (not by tho contributors, so no +). – BaRud Aug 19 '20 at 11:01
  • I was also looking for some interactive way to edit a matplot plots and even if this answer is 3 yo, it is actually solving the issue. – toto Aug 18 '23 at 07:43
1

One way to do what (I think) you ask for is to use ipython. ipython is an interactive python environment which comes with many python distributions.

A quick example:

In a cmd, type >>> ipython, which will load the ipython terminal. In ipython, type:

import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1)
ax.plot([1, 2, 3, 4, 5], [1, 2, 3, 4, 5], 'r-')
fig.show()

Now you have a figure, at the same time as the ipython terminal is "free". This means that you can do commands to the figure, like ax.set_xlabel('XLABEL'), or ax.set_yticks([0, 5]). To make it show on screen, you need to redraw the canvas, which is done by calling fig.canvas.draw().

Note that with ipython, you have full tab-completion with all functions to all objects! Typing fig.get_ and then tab gives you the full list of functions beginning with fig.get_, this is extremely helpful!

Also note that you can run python-scripts in ipython, with run SCRIPT.py in the ipython-cmd, and at the same time having access to all variables defined in the script. They can then be used as above.

Hope this helps!

pathoren
  • 1,634
  • 2
  • 14
  • 22
1

No, it is not generally possible to do what you want (dynamically interact with a matplotlib using the mouse). What you see is a rendering of your plot on a "canvas", but it does not include a graphical user interface (GUI) like you have with e.g. xmgrace, Origin etc.

That being said, if you wish to pursue it you have a number of possible options, including:

But it is probably quicker and more convenient to just use some other plotting software, where someone has already designed a decent user interface for you.

Alternatively, using an iPython notebook to quickly modify your plot script works well enough.

Community
  • 1
  • 1
feedMe
  • 3,431
  • 2
  • 36
  • 61
0

There is a navigation toolbar in qt4agg matplotlib backend which you can add easily. Not much, but at least good scaling...

![enter image description here

Not a working code, just some fragments:

from matplotlib.backends.backend_qt4agg import FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
from matplotlib.backends.qt_compat import QtCore, QtWidgets, is_pyqt5

self.figure = Figure(figsize=(5, 3))
self.canvas = FigureCanvas(self.figure)
self.addToolBar(QtCore.Qt.BottomToolBarArea,
                NavigationToolbar(self.canvas, self))

Self is your window object derived from QtGui.QMainWindow.

Mikhail M
  • 929
  • 2
  • 10
  • 23