Programing

IPython 노트북에서 대화식 matplotlib 창을 어떻게 열 수 있습니까?

crosscheck 2020. 8. 3. 17:39
반응형

IPython 노트북에서 대화식 matplotlib 창을 어떻게 열 수 있습니까?


필자는 IPython을 사용 --pylab=inline하고 있으며 때로는 플롯 (터미널 파이썬 콘솔에서 무언가를 플롯 할 때 팝업되는)을보기 위해 대화 형의 확대 가능한 matplotlib GUI로 빠르게 전환하고 싶습니다. 내가 어떻게 할 수 있습니까? 노트북을 나가거나 다시 시작하지 않는 것이 좋습니다.

IPy 노트북의 인라인 플롯의 문제점은 해상도가 제한되어 있으며 더 작은 부분을 보려면 확대 할 수 없다는 것입니다. 터미널에서 시작하는 maptlotlib GUI를 사용하면 확대하려는 그래프의 사각형을 선택할 수 있으며 축은 그에 따라 조정됩니다. 나는 실험을 시도했다

from matplotlib import interactive
interactive(True)

interactive(False)

그러나 그것은 아무것도하지 않았습니다. 온라인에서도 힌트를 찾을 수 없습니다.


설명서 에 따르면 다음 과 같이 전환 할 수 있어야합니다.

In [2]: %matplotlib inline 
In [3]: plot(...)

In [4]: %matplotlib qt  # wx, gtk, osx, tk, empty uses default
In [5]: plot(...) 

그러면 일반 플롯 창이 나타납니다 (노트북을 다시 시작해야 할 수도 있습니다).

이게 도움이 되길 바란다.


인라인 플롯에서 대화식으로 전환하고 다시 이동 (확대 / 축소 가능)하는 것만으로도 % matplotlib 매직을 사용하는 것이 좋습니다.

#interactive plotting in separate window
%matplotlib qt 

html로 돌아 가기

#normal charts inside notebooks
%matplotlib inline 

% pylab magic은 다른 많은 것들을 가져오고 충돌을 일으킬 수도 있습니다. "pylab import *에서"를 수행합니다.

또한 새로운 노트북 백엔드를 사용할 수 있습니다 (matplotlib 1.4에 추가됨).

#interactive charts inside notebooks, matplotlib 1.4+
%matplotlib notebook 

차트에서 더 많은 대화 형 기능을 원하면 mpld3bokeh를 볼 수 있습니다 . mpld3은 데이터 포인트가 많지 않고 (예 : <5k +) 일반 matplotlib 구문을 사용하고 싶지만 % matplotlib notebook과 비교하여 더 많은 대화 형 작업을 원할 경우 유용합니다. Bokeh는 많은 데이터를 처리 할 수 ​​있지만 별도의 라이브러리이므로 구문을 알아야합니다.

또한 pivottablejs를 확인할 수 있습니다 (pip install pivottablejs)

from pivottablejs import pivot_ui
pivot_ui(df)

그러나 멋진 대화식 데이터 탐색은 재현성을 완전히 망칠 수 있습니다. 그것은 나에게 일어 났으므로 아주 초기 단계에서만 사용하고 데이터에 대한 느낌을 얻은 후에 순수한 인라인 matplotlib / seaborn으로 전환하려고합니다.


matplotlib 1.4.0부터는 노트북에서 사용할 수있는 대화식 백엔드가 있습니다.

%matplotlib notebook

별명을 등록하지 않은 IPython의 몇 가지 버전이 있습니다. 폴백은 다음과 같습니다.

%matplotlib nbagg

그래도 작동하지 않으면 IPython을 업데이트하십시오.

이것을 사용하려면 tmpnb.org로 이동하십시오.

붙여 넣기

%matplotlib notebook

import pandas as pd
import numpy as np
import matplotlib

from matplotlib import pyplot as plt
import seaborn as sns

ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts = ts.cumsum()

df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index,
                  columns=['A', 'B', 'C', 'D'])
df = df.cumsum()
df.plot(); plt.legend(loc='best')    

코드 셀에 (또는 기존 파이썬 데모 노트북 수정)


Anaconda의 "jupyter QTConsole"에서 ipython을 www.continuum.io/downloads에서 5/28/20117에 사용하고 있습니다.

다음은 ipython magic을 사용하여 별도의 창과 인라인 플롯 모드 사이를왔다 갔다하는 예제입니다.

>>> import matplotlib.pyplot as plt

# data to plot
>>> x1 = [x for x in range(20)]

# Show in separate window
>>> %matplotlib
>>> plt.plot(x1)
>>> plt.close() 

# Show in console window
>>> %matplotlib inline
>>> plt.plot(x1)
>>> plt.close() 

# Show in separate window
>>> %matplotlib
>>> plt.plot(x1)
>>> plt.close() 

# Show in console window
>>> %matplotlib inline
>>> plt.plot(x1)
>>> plt.close() 

# Note: the %matplotlib magic above causes:
#      plt.plot(...) 
# to implicitly include a:
#      plt.show()
# after the command.
#
# (Not sure how to turn off this behavior
# so that it matches behavior without using %matplotlib magic...)
# but its ok for interactive work...

문제에 대한 더 나은 해결책은 차트 라이브러리 일 수 있습니다 . 뛰어난 Highcharts 자바 스크립트 라이브러리를 사용하여 아름답고 대화식 음모를 꾸밀 수 있습니다 . Highcharts는 HTML svg태그를 사용하므로 모든 차트는 실제로 벡터 이미지입니다.

일부 기능 :

  • Vector plots which you can download in .png, .jpg and .svg formats so you will never run into resolution problems
  • Interactive charts (zoom, slide, hover over points, ...)
  • Usable in an IPython notebook
  • Explore hundreds of data structures at the same time using the asynchronous plotting capabilities.

Disclaimer: I'm the developer of the library


Restart kernel and clear output (if not starting with new notebook), then run

%matplotlib tk

For more info go to Plotting with matplotlib

참고URL : https://stackoverflow.com/questions/14261903/how-can-i-open-the-interactive-matplotlib-window-in-ipython-notebook

반응형