为什么全视角不显示Spyder中的直方图?

时间:2019-03-17 14:18:33

标签: python python-3.x spyder holoviews

我在Windows上安装了Anaconda 3.7。它在Jupyter中起作用,但在Spyder中不起作用。 这段代码:

import holoviews as hv
import pandas as pd
import numpy as np
output_notebook ()
flora = pd.read_csv ('iris.csv')
hv.extension('bokeh')
frequencies, edges = np.histogram(flora['petal width'], bins = 5)
print(frequencies, edges)
hv.Histogram(frequencies, edges, label = 'Histogram')

仅返回值:

[49  8 41 29 23] [0.1  0.58 1.06 1.54 2.02 2.5 ]
WARNING:root:Histogram: Histogram edges should be supplied as a tuple along with the values, passing the edges will be deprecated in holoviews 2.0.

是否可以在Spyder中看到直方图?

3 个答案:

答案 0 :(得分:4)

holoviews的优点在于,它允许您在基于浏览器的现代 bokeh 和历史悠久的 matplotlib 之间进行选择,以显示其图(以及 plotly 进行扩展,主要用于3D绘图。

Spyder能够以内联方式(即在python控制台中或自从最近以来是其新的绘图面板)或交互式地(即在弹出窗口-several backends exist中,在所有qt中)呈现matplotlib图。您可以通过在spyder ipython控制台中输入import subprocess process = subprocess.Popen(['tree','D://'], stdout=PIPE, stderr=PIPE) stdout, stderr = process.communicate() %matplotlib inline在这些之间进行切换。

这些后端将成为您的holoview生成的matplotlib绘图所在的地方!

现在,您需要明确地告诉holoviews使用matplotlib作为渲染图的后端(我在下面将其称为holoview_object可以是它们所谓的“元素”,也可以是以下各项的组合:布局,覆盖图,全息地图。 ..)。您可以使用

%matplotlib qt

,然后创建一个空的matplotlib图形,并破解其管理器以在默认的matplotlib后端中显示该图形:

matplotlib_fig = holoviews.render(holoview_object, backend='matplotlib')

使用上述概念,我为自己提供了一些实用程序功能,可以直接或从holoviews对象开始从spyder内轻松显示matplotlib或bokeh图,随时使用它们:

dummy = plt.figure()
new_manager = dummy.canvas.manager
new_manager.canvas.figure = matplotlib_fig
fig.set_canvas(new_manager.canvas)

总结: 如果您希望在spyder绘图窗格中静态地绘制绘图(如果不使用其绘图窗格,则为python控制台),请执行以下操作:

import matplotlib.pyplot as plt
import bokeh as bk
import holoviews as hv

def mplshow(fig):

    # create a dummy figure and use its
    # manager to display "fig"

    dummy = plt.figure()
    new_manager = dummy.canvas.manager
    new_manager.canvas.figure = fig
    fig.set_canvas(new_manager.canvas)

def bkshow(bkfig, title=None, save=0, savePath='~/Downloads'):
    if title is None: title=bkfig.__repr__()
    if save:bk.plotting.output_file(f'{title}.html')
    bk.plotting.show(bkfig)

def hvshow(hvobject, backend='matplotlib', return_mpl=True):
    '''
    Holoview utility which
    - for dynamic display, interaction and data exploration:
        in browser, pops up a holoview object as a bokeh figure
    - for static instanciation, refinement and data exploitation:
        in matplotlib current backend, pops up a holoview object as a matplotlib figure
        and eventually returns it for further tweaking.
    Parameters:
        - hvobject: a Holoviews object e.g. Element, Overlay or Layout.
        - backend: 'bokeh' or 'matplotlib', which backend to use to show figure
        - return_mpl: bool, returns a matplotlib figure
        
    '''
    assert backend in ['bokeh', 'matplotlib']
    if backend=='matplotlib' or return_mpl:
        mplfig=hv.render(hvobject, backend='matplotlib')
    if backend=='bokeh': bkshow(hv.render(hvobject, backend='bokeh'))
    elif backend=='matplotlib': mplshow(mplfig)
    if return_mpl: return mplfig

如果您希望在交互式qt窗口中弹出图 ,请执行以下操作:

%matplotlib inline
hvshow(holoviews_object, 'matplotlib')

如果您希望以交互方式在浏览器中(例如散景)弹出您的情节 ,请执行以下操作:

%matplotlib qt
hvshow(holoviews_object, 'matplotlib')

我喜欢spyder(远远超过jupyter笔记本),也很喜欢可以同时使用Holyviews!

答案 1 :(得分:2)

(此处为 Spyder维护者),Holoviews生成要在网络浏览器中呈现的内容,抱歉,Spyder控制台目前无法显示该内容。

答案 2 :(得分:1)

作为一种解决方法,可以通过将Holoviews图放在Panel对象中并在其上调用.show()来在浏览器中打开图。
库面板可用于在浏览器中创建带有Holoviews图形的仪表板。
这是一个工作示例:

# library imports
import numpy as np
import pandas as pd
import holoviews as hv
hv.extension('bokeh', logo=False)
import panel as pn

# create sample data
data = np.random.normal(size=[50, 2])
df = pd.DataFrame(data, columns=['col1', 'col2'])

# create holoviews graph
hv_plot = hv.Points(df)

# display graph in browser
# a bokeh server is automatically started
bokeh_server = pn.Row(hv_plot).show(port=12345)

# stop the bokeh server (when needed)
bokeh_server.stop()

另请参阅:How do i get my interactive Holoviews graph to display in Visual Studio (without Jupyter)?