简体   繁体   English

在Jupyter Notebook中同时使用Matplotlib内联和QT

[英]Use both matplotlib inline and qt in jupyter notebook

I am using Jupyter (with IPython) to analyze research data, as well as export figures. 我正在使用Jupyter(与IPython一起使用)来分析研究数据以及导出数据。 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. 我真的很喜欢Jupyter提供的笔记本方法:长时间浏览实验后,我可以轻松地看到数字与数据的对应关系。 This is of course using the inline backend. 当然,这是使用内联后端。

However, when I want to explore new data, I prefer to use the QT backend. 但是,当我想探索新数据时,我更喜欢使用QT后端。 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. 它比嵌入式方法快,并且可以轻松缩放,放大和缩小,并在左下角很好地显示X和Y坐标。 Moreover, I can use the QT backend to determine good x and y limits to use in the inline backend. 此外,我可以使用QT后端来确定要在串联后端中使用的良好x和y限制。

I have tried using the %matplotlib notebook magic, but it is simply too slow. 我已经尝试过使用%matplotlib notebook魔术,但是它太慢了。 For some experiments I am plotting ~500 spectra (each consists of ~1000 data points), which is already slow in the inline backend. 对于某些实验,我正在绘制〜500个光谱(每个光谱由〜1000个数据点组成),这在嵌入式后端中已经很慢了。 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. 因此,每当我绘图时,我都想同时使用QT后端和内联后端。 (So, whenever I execute a cell which plots data, it should both display the inline image, and pop up a QT backend window). (因此,每当我执行绘制数据的单元格时,它都应显示嵌入式图像并弹出QT后端窗口)。 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? 有没有办法做到这一点?

This allows you to run QtConsole, plotting with the plotSin function, both inline and through the QtConsole. 这使您可以内联并通过QtConsole使用plotSin函数运行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

在此处输入图片说明

%connect_info # For your own connection
%qtconsole

QtConsole opens and now you can call your function to plot externally.. QtConsole打开,现在您可以调用函数在外部进行绘图。

在此处输入图片说明

Using %matplotlib qt allows for printing in a loop, but unfortunately it seems to overlap the plots. 使用%matplotlib qt可以循环打印,但不幸的是,它似乎与图形重叠。 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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM