3

I am using Jupyter (with IPython) to analyze research data, as well as export figures. I really like the notebook approach offered by Jupyter: when I revisit an experiment after a long time, I can easily see how the figures correspond to the data. This is of course using the inline backend.

However, when I want to explore new data, I prefer to use the QT backend. It is faster than the inline one, and allows to easily scale, zoom in and out, and nicely displays the X and Y coordinates in the bottom left corner. Moreover, I can use the QT backend to determine good x and y limits to use in the inline backend.

I have tried using the %matplotlib notebook magic, but it is simply too slow. For some experiments I am plotting ~500 spectra (each consists of ~1000 data points), which is already slow in the inline backend. Even with less data points, the notebook backend is just too slow to use.

Therefore, I would like to use both the QT backend, and the inline backend whenever I plot something. (So, whenever I execute a cell which plots data, it should both display the inline image, and pop up a QT backend window). This way, I still have a nice overview of plots in my notebook, while also allowing me to easily explore my data. Is there a way to achieve this?

3 Answers 3

3

As far as I know it is not possible to use more than one backend at once. What I do is to switch backend and replot. I have done it with matplotlib.use() or %matplotlib <backend>, according to which version or environment I was working in. I had the impression that this sometimes doesn't work or maybe it is not responsive, so I need to restart the environment. I imagine it is not easy to maintain it, so I am even surprised it works so smoothly almost always.

Available backends can be listed with:

matplotlib.rcsetup.interactive_bk
matplotlib.rcsetup.non_interactive_bk
matplotlib.rcsetup.all_backends

Hopefully here there are other useful info: https://matplotlib.org/stable/tutorials/introductory/usage.html

Sign up to request clarification or add additional context in comments.

Comments

0

This allows you to run QtConsole, plotting with the plotSin function, both inline and through the QtConsole.

import matplotlib
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline  

..

def plotChirp(Type, Exp, Rand):

    # Orignal Chirp Funciton From:
    # http://stackoverflow.com/questions/19410042/how-to-make-ipython-notebook-matplotlib-plot-inline
    x = np.linspace(0, 3*np.pi, Rand)
    plt.plot(x, np.sin(x**int(Exp)))
    plt.title('A simple chirp ' + Type)
    plt.show()

..

plotChirp("A", 5, 200) # Plots inline if you choose

enter image description here

%connect_info # For your own connection
%qtconsole

QtConsole opens and now you can call your function to plot externally..

enter image description here

Using %matplotlib qt allows for printing in a loop, but unfortunately it seems to overlap the plots. Looking into subplots as a possible solution.

%matplotlib qt
for i in range(0,2):

    if i == 0:
        plotChirp("B",1, 400)
    else:
        plotChirp("c",6, 1000)

enter image description here

3 Comments

Thank you for your answer. However, this is not the QT GUI window I was looking for. I want the same window that pops up when one uses %matplotlib qt, not the QT-console (which does not have the nice scaling, zooming and axis property changing capabilities of the other window).
You may want to check out this post, as it allows for multiple plots to be scrolled through in QT Gui using a scroll though function: stackoverflow.com/questions/11563295/…
@tcpie i think this is what you are looking for stackoverflow.com/questions/41046299/… it worked for me %matplotlib tk
0

One thing you can try, which I recently found works (at least in VSCode Notebooks) is the following:

  1. use %matplotlib qt as others have mentioned, so when you run code in a cell that creates a plot of some data, it should open in an external figure window that allows you to zoom, pan, etc.

  2. create a handle to your figure, for example using fig = plt.figure(figsize=(8,4))

  3. create the plot and explore in the external figure window as needed

  4. in a new cell, simply type and run fig (nothing else in the cell) .... now in that cell the output should embed the current view of your figure back into the Notebook

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.