maplotlib.pyplot.show()适用于python,但不适用于jupyter控制台 [英] maplotlib.pyplot.show() works with python but not with jupyter console
问题描述
我查看了这个问题和
I have looked at this question and also this one. It looks like for me, matplotlib.pyplot.show() shows a figure from python, but not from jupyter console.
matplotlib.matplotlib_fname()
为两者返回相同的matplotlibrc文件位置.
matplotlib.matplotlib_fname()
returns the same matplotlibrc file location for both.
但是,当我尝试找到与matplotlib.rcParams['backend']
一起使用的后端时,jupyter控制台会告诉我-'module://ipykernel.pylab.backend_inline'
,无论我修改了要使用的matplotlibrc文件.
However, when I try to find the backend being used with matplotlib.rcParams['backend']
jupyter console tells me - 'module://ipykernel.pylab.backend_inline'
, regardless of which backend I have modified the matplotlibrc file to use.
Python可以正确显示我正在使用的后端;目前为"TkAgg".
Python on the other hand, correctly shows the backend I'm using; currently 'TkAgg'.
我使用python -mpip install -U matplotlib
安装了matplotlib.
I installed matplotlib using python -mpip install -U matplotlib
.
我正在使用以下版本:
- Windows 10
- Jupyter控制台5.2.0
- Python 2.7.14
- IPython 5.5.0
我可以使用python来解决问题,但也可以在jupyter控制台中弄清楚这一点.
I can make do with using python, but it would be nice to figure this out for jupyter console as well.
推荐答案
首先请注意,plt.show()
的工作原理与预期一样,在Juypter中也是如此.
First note that plt.show()
works as expected, also in Juypter.
这使用默认的'module://ipykernel.pylab.backend_inline'
后端.此后端由Jupyter设置,与rcParams
设置无关.
This uses the default 'module://ipykernel.pylab.backend_inline'
backend. This backend is set by Jupyter, independently of the rcParams
setting.
您可以使用matplotlib.use()
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show()
或仅使用IPython魔术%matplotlib backendname
or just using IPython magic %matplotlib backendname
%matplotlib tk
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show()
您可以使用pyplot.switch_backend()
plt.switch_backend("TkAgg")
plt.plot([1,2,3])
plt.show()
或使用相同的IPython魔术
or using the same IPython magic
%matplotlib tk
plt.plot([1,2,3])
plt.show()
如果您想将后端设置为默认使用,请参见以下问题: 在Jupyter Ipython中更改matplotlib的默认后端
If you want to set the backend to be used by default, see this question: Change default backend for matplotlib in Jupyter Ipython
这篇关于maplotlib.pyplot.show()适用于python,但不适用于jupyter控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!