BaRud
BaRud

Reputation: 3218

is it possible to edit matplotlib plot interactively?

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)?

Upvotes: 4

Views: 6078

Answers (4)

Anton Kühl
Anton Kühl

Reputation: 71

You can do simple interactive editing with pylustrator

pip install pylustrator

Upvotes: 7

Mikhail  M
Mikhail M

Reputation: 960

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.

Upvotes: 0

feedMe
feedMe

Reputation: 3727

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.

Upvotes: 1

pathoren
pathoren

Reputation: 1666

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!

Upvotes: 1

Related Questions