如何在Python中使用Bokeh查看Holoviews的opts?

7

我一直在使用Holoview生成一些漂亮的图表。我想知道像hv.Curve这样的东西在我使用opts时有哪些选项可用。几乎没有什么文档。Holoview的帮助页面没有太大帮助。其中写道:"要获取完整文档和可用的样式和图表选项,请使用hv.help(hv.Curve)。" 但它并没有解释如何使用这个命令。我已经尝试在我的代码中运行它。我试过在终端中运行该命令。我还尝试了使用print()help()来运行它。我只想知道除了legend_position之外,我可以在这里放什么:overlay.opts(legend_position='right')?下面是我尝试使hv.help(hv.Curve)命令工作的代码。我知道这很荒谬:

#!/usr/bin/env python3

import holoviews as hv
import numpy as np
import scipy.special
import pandas as pd
import bokeh
import pandas as pd
import numpy as np
import bokeh as bk
import plotly.graph_objects as go
import plotly.express as pex
import holoviews.plotting.bokeh
import numpy as np
import pandas as pd
from holoviews import *
from holoviews import Store
from bokeh.plotting import show
from holoviews import opts
from holoviews import Store
from holoviews import dim, opts

hv.help(hv.Curve)

以下是我收到的错误信息:

        backend_registry = cls.store.registry.get(backend, {})
AttributeError: 'NoneType' object has no attribute 'registry'

感谢任何帮助,如果需要更多信息,请让我知道,因为这是我在这个网站上的第一篇帖子。

3个回答

4

如果想要了解.opts()以及它们的使用,我认为这个页面最好:
http://holoviews.org/user_guide/Customizing_Plots.html

经过查阅,至少有以下通用绘图选项:

  • .opts(title="自定义标题")
  • .opts(bgcolor='浅灰色')
  • .opts(fontsize={'title': 16, 'labels': 14, 'xticks': 6, 'yticks': 12})
  • .opts(fontscale=2.)
  • .opts(legend_position='bottom_right', legend_offset=(0, 200))
  • .opts(width=500, height=400)
  • .opts(legend_muted=True, legend_cols=2)
  • .opts(hooks=[hook])(自定义修改绘图,请参见页面上的文档)
  • .opts(logx=True, logy=True)
  • .opts(shared_axes=False)(让多个绘图共享相同的 x 轴或 y 轴)
  • .opts(invert_axes=True)(将 x 轴变成 y 轴,反之亦然)
  • .opts(labelled=[])(在 x 轴或 y 轴上不显示标签)
  • .opts(xlabel='自定义x轴标签')
  • .opts(padding=0.1) 在您的绘图中添加额外的空间(=填充)
  • .opts(xlim=(-10, 110), ylim=(-14, 6)) 更改 x 范围和 y 范围
  • .opts(xticks=10) 或者 .opts(xticks=[0, 50, 100])
  • .opts(xticks=[(0, '零'), (50, '五十'), (100, '一百')])
  • .opts(yformatter='$%.2f') 添加特殊的轴格式化程序
  • .opts(xrotation=45) 旋转刻度标签

有些选项更具体,例如散点图的设置,但是对于这些选项最好检查该特定绘图的页面:

(errorbars * overlay * curve).opts(
    opts.ErrorBars(line_width=5), 
    opts.Scatter(jitter=0.2, alpha=0.5, size=6, height=400, width=600)
)

回答你最初的问题:

你使用的是哪个版本的HoloViews?
我使用的是1.13.5,在Jupyter笔记本中,当我执行:hv.help(hv.Curve)时,可以看到下面的截图,这些是你可以在hv.Curve()中使用的参数,但我认为它们对你的问题并不是很有帮助:

hv help of hv curve


4

这里涉及到几个问题。首先,HoloViews 中的选项取决于您选择的绘图库后端,如 bokeh、matplotlib 或 plotly。在您的代码示例中,您尚未加载绘图库后端,因此无法获得任何关于 legend_position 等绘图选项的帮助。当然,您不应该得到像 AttributeError 这样的错误消息,请在 https://github.com/holoviz/holoviews/issues 提交问题以帮助我们复制该虚假消息。当我运行没有加载绘图库后端的 hv.help(hv.Curve) 时,我并没有看到任何错误消息;而是得到了 Sander 报告的一个简短的列表,其中列出的选项与绘图无关,应该始终可用,无论使用哪种后端。

$ python
>>> import holoviews as hv
>>> hv.help(hv.Curve)
Parameters of 'Curve'
=====================

Parameters changed from their default values are marked in red.
Soft bound values are marked in cyan.
C/V= Constant/Variable, RO/RW = ReadOnly/ReadWrite, AN=Allow None

Name                        Value                     Type     Bounds   Mode 

cdims                   OrderedDict()                 Dict              V RW 
datatype   ['dataframe', 'dictionary', 'grid', '...   List   (0, None)  V RW 
extents            (None, None, None, None)          Tuple              V RW 
group                      'Curve'                   String             C RW 
kdims                  [Dimension('x')]               List     (1, 2)   V RW 
label                         ''                     String             C RW 
vdims                  [Dimension('y')]               List   (1, None)  V RW 

Parameter docstrings:
=====================

cdims:    The constant dimensions defined as a dictionary of Dimension:value
          pairs providing additional dimension information about the object.
...          
vdims:    The value dimensions of the Chart, usually corresponding to a
          number of dependent variables.

正如您所见,这7个选项在这种情况下都没有帮助。如果加载一个绘图后端,您将获得更多的选项:

>>> hv.extension("bokeh")
>>> hv.help(hv.Curve)
Curve

Online example: http://holoviews.org/reference/elements/bokeh/Curve.html

-------------
Style Options
-------------

    alpha, color, hover_alpha, hover_color, hover_line_alpha, hover_line_color, line_alpha, line_cap, line_color, line_dash, line_join, line_width, muted, muted_alpha, muted_color, muted_line_alpha, muted_line_color, nonselection_alpha, nonselection_color, nonselection_line_alpha, nonselection_line_color, selection_alpha, selection_color, selection_line_alpha, selection_line_color, visible

(Consult bokeh's documentation for more information.)

------------
Plot Options
------------

The plot options are the parameters of the plotting class:

Parameters of 'CurvePlot'
=========================

Parameters changed from their default values are marked in red.
Soft bound values are marked in cyan.
C/V= Constant/Variable, RO/RW = ReadOnly/ReadWrite, AN=Allow None

Name                                 Value                         Type         Bounds     Mode  

active_tools                           []                          List       (0, None)    V RW  
... 57 other options...
zlim                               (nan, nan)                     Tuple                    V RW  

Parameter docstrings:
=====================

active_tools:      Allows specifying which tools are active by default. Note
                   that only one tool per gesture type can be active, e.g.
                   both 'pan' and 'box_zoom' are drag tools, so if both are
                   listed only the last one will be active.
... 57 other options...
zlim:              User-specified z-axis range limits for the plot, as a tuple (low,high).
                   If specified, takes precedence over data and dimension ranges.

这里只列出了"style"选项,没有详细说明,因为它们直接传递给底层绘图库,所以无法在此处记录; 在这种情况下,请参阅 Bokeh。其他选项具有文档字符串,因为它们由 HoloViews 实现。

即使这样,如果您查看输出,您会注意到legend_position实际上并不在任何一个列表中。这是因为legend_position不是hv.Curve的选项,而是hv.Overlay的选项,后者是用于叠加的通用容器(包括曲线和许多其他对象)。因此,您需要对 Overlay 类执行hv.help

>>> hv.help(hv.Overlay)
Overlay

Online example: http://holoviews.org/reference/containers/bokeh/Overlay.html

-------------
Style Options
-------------

    background_fill_alpha, background_fill_color, border_alpha, border_color, border_hover_alpha, border_hover_color, border_hover_line_alpha, border_hover_line_color, border_line_alpha, border_line_cap, border_line_color, border_line_dash, border_line_join, border_line_width, border_muted_alpha, border_muted_color, border_muted_line_alpha, border_muted_line_color, border_nonselection_alpha, border_nonselection_color, border_nonselection_line_alpha, border_nonselection_line_color, border_selection_alpha, border_selection_color, border_selection_line_alpha, border_selection_line_color, click_policy, glyph_height, glyph_width, label_height, label_standoff, label_width, legend_padding, legend_spacing, text_align, text_alpha, text_baseline, text_color, text_font, text_font_size, text_font_style

(Consult bokeh's documentation for more information.)

------------
Plot Options
------------

The plot options are the parameters of the plotting class:

Parameters of 'OverlayPlot'
===========================

Parameters changed from their default values are marked in red.
Soft bound values are marked in cyan.
C/V= Constant/Variable, RO/RW = ReadOnly/ReadWrite, AN=Allow None

Name                                 Value                         Type         Bounds     Mode  

active_tools                           []                          List       (0, None)    V RW  
...66 more options...
zlim                               (nan, nan)                     Tuple                    V RW  

Parameter docstrings:
=====================

active_tools:      Allows specifying which tools are active by default.
...
legend_position:   Allows selecting between a number of predefined legend position
                   options. The predefined options may be customized in the
                   legend_specs class attribute.
...

所以你可以看到,legend_position是一个选项,但最终仍然没有告诉你什么是允许的。令人沮丧!在许多情况下,您可以在Jupyter中输入部分内容并调用tab完成,但在这种带有字符串参数的情况下,最好的方法是提供一个不正确的值。如果您在Jupyter中这样做,您将得到一个允许选项的列表:

>>> hv.Overlay().opts(hv.opts.Overlay(legend_position='aslkjf'))
ValueError: aslkjf not in Parameter legend_position's list of possible objects, valid options include [top_right, top_left, bottom_left, bottom_right, right, left, top, bottom]

有很多信息可供使用,但很难找到它!


当我尝试理解其他选项的正确语法时,我遇到了同样的问题。扩展hv.help函数以便我们可以在带有参数的对象上调用它并获取预期的表达式是否会很复杂?例如(hv.help(hv.Overlay,option='legend_position'))。这对于绘图样式选项也可能非常有用。 - lhoupert
哦,好主意!对于像图例位置这样的绘图选项,HoloViews 中提供了完整的信息,因此我们应该完全支持这样的调用,其中显示有关参数的所有信息。我认为我们还可以通过查询 Bokeh 的 JSON 信息来处理样式选项。您能否在 https://github.com/holoviz/holoviews/issues 上打开一个问题,请求该功能? - James A. Bednar
@JamesA.Bednar,您认为Python版本是否有影响?我正在使用3.8.12,但仍然遇到相同的注册表错误:
import holoviews as hv hv.extension('bokeh') hv.help(hv.Curve) Traceback (most recent call last): ...(省略大部分错误消息) backend_registry = cls.store.registry.get(backend, {}) AttributeError: 'NoneType' object has no attribute 'registry'
- user3711502
不,Python版本不应该有影响。我刚刚在我的Macbook上使用“conda env create -n py38 python=3.8.12 holoviews”测试了这些命令,并且它们可以正常工作,所以可能存在某些平台特定的问题?请在github.com/holoviz/holoviews/issues上提交一个问题,指定您的平台和任何其他可能重要的细节,以复制观察到的错误。 - James A. Bednar

0

当我只安装了 holoviews 的虚拟环境时,我遇到了相同的错误。 我安装了 jupyterlab 来检查问题是否存在。

在 jupyterlab 中,帮助文档显示正常。

当我再次尝试回到普通的 Python REPL 时,它也可以正常工作了。

所以看起来可能是缺少某些依赖项。 一个简单的解决方法就是 安装 jupyterlab

如果你愿意花时间,你可以逐个检查 jupyterlab 的依赖项,直到找到所需的那个。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接