简体   繁体   English

在jupyter笔记本中将Matplotlib与PyQt5一起使用

[英]use matplotlib with PyQt5 in jupyter notebook

I need to create scientific plots with matplotlib in Jupyter notebook, and create them in separate windows instead of inline. 我需要在Jupyter笔记本中使用matplotlib创建科学图,并在单独的窗口而不是内联窗口中创建它们。 I ran %matplotlib qt and got this error: ImportError: No module named PyQt4 我运行%matplotlib qt并收到以下错误: ImportError: No module named PyQt4

A lot of related answers on the internet involves installing PyQt4. 互联网上许多相关的答案都涉及到安装PyQt4。 To give some background information, I use Anaconda to manage python modules on a Windows machine. 为了提供一些背景信息,我使用Anaconda在Windows计算机上管理python模块。 Anaconda installs PyQt5.6.0 in its root environment by default. Anaconda默认在其根环境中安装PyQt5.6.0。 Downgrading this package to PyQt4 will solve the problem, however, another module in my project depends on PyQt5. 将该软件包降级到PyQt4将解决该问题,但是,我项目中的另一个模块取决于PyQt5。 I want to figure out how use matplotlib with PyQt5. 我想弄清楚如何在PyQt5中使用matplotlib。

According to matploblib's tutorial , it supports PyQt5 backend. 根据matploblib的教程 ,它支持PyQt5后端。 I tried to run matplotlib.rcParams['backend'] = "Qt5Agg" and matplotlib.use('Qt5Agg') before the %matplotlib qt , but got same error. 我试图在%matplotlib qt之前运行matplotlib.rcParams['backend'] = "Qt5Agg"matplotlib.use('Qt5Agg') ,但是出现了相同的错误。 Am I missing something that's so obvious to others? 我是否错过了其他人这么明显的东西? Helps are appreciated. 帮助表示赞赏。

Thanks to @ImportanceOfBeingErnest (see comments below the question), I figured it out myself. 感谢@ImportanceOfBeingErnest(请参阅问题下方的注释),我自己弄清楚了。 Here is the answer for anyone who stumbled upon this question. 对于那些偶然发现此问题的人,这是答案。

The method that should work for most people if you want to use PyQt5 in matplotlib to create figures in individual windows is to run matplotlib.use('Qt5Agg') then %matplotlib qt5 . 如果要在matplotlib中使用PyQt5在各个窗口中创建图形,则对于大多数人来说应该适用的方法是运行matplotlib.use('Qt5Agg')然后运行%matplotlib qt5

This doesn't work for me for some reason I still don't know. 由于某些我仍然不知道的原因,这对我不起作用。 But the workaround is to check the following two files: /.ipython/ipython_config.py and /.ipython/ipython_kernel_config.py and make sure there is no backend preference in it. 但是解决方法是检查以下两个文件:/. /.ipython/ipython_config.py/.ipython/ipython_kernel_config.py并确保其中没有后端首选项。 In my case, I didn't even have these two files. 就我而言,我什至没有这两个文件。 The problem was solved after I created them. 创建它们后,问题就解决了。 You can create them by running ipython profile create in cmd. 您可以通过在cmd中运行ipython profile create来创建它们。

If anyone have more insights on why these two files (with no uncommented content) are needed, please let me know! 如果有人对为什么需要这两个文件(无注释内容)有更多的了解,请告诉我!

In my ~/.config/matplotlib/matplotlibrc file at line 41 I have the following: 在第41行的〜/ .config / matplotlib / matplotlibrc文件中,我具有以下内容:

backend : Qt5Agg 后端:Qt5Agg

then I usually type 然后我通常输入

matplotlib qt in a notebook or qtconsole to get external, as opposed to inline, plots 在笔记本电脑或qtconsole中使用matplotlib qt获取外部(而不是嵌入式)图

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

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