如何在Python中使用Bokeh查看HoloView的选项

2024-04-27 03:59:13 发布

您现在位置:Python中文网/ 问答频道 /正文

一直在使用holoview生成一些整洁的绘图。我想看看当我使用opts时,我对hv.Curve之类的东西有什么选择。几乎没有任何文件。{a1}没有帮助。在它上面写着“对于完整的文档以及可用的样式和打印选项,请使用^{”。但是没有解释如何使用它。我试着把它作为代码的一部分来运行。我已尝试在终端中运行该命令。我尝试了print(),并使用help()运行。我只想知道我能在这里放些什么:overlay.opts(legend_position='right')除了legend_position?下面是我试图让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'

谢谢你的帮助,让我知道如果我可以提供更多的信息,因为这是我在这个网站上的第一篇文章


Tags: 代码fromimportnumpypandasasnpbokeh
2条回答

这里涉及到几个问题。首先,HoloView中的选项取决于您选择的打印库后端,例如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.

这里只列出了“样式”选项,没有记录,因为它们直接传递到底层打印库,因此不能在这里记录;见本例中的Bokeh。其他选项有docstring,因为它们是由HoloView实现的

即使如此,如果您查看输出,您会注意到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 completion,但在本例中,使用字符串参数时,最好只需提供错误的值。如果您在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]

有很多信息,但很难找到

对于.opts()及其使用的一个很好的概述,我认为这个页面是最好的:
http://holoviews.org/user_guide/Customizing_Plots.html

如果我仔细查看,至少有以下常规绘图选项:

  • .opts(title="Custom Title")
  • .opts(bgcolor='lightgray')
  • .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='Custom x-label')
  • .opts(padding=0.1)在绘图中添加额外的空间(=填充)
  • .opts(xlim=(-10, 110), ylim=(-14, 6))更改x范围和y范围
  • .opts(xticks=10)or.opts(xticks=[0,50,100])
  • .opts(xticks=[(0, 'zero'), (50, 'fifty'), (100, 'one hundred')])
  • .opts(yformatter='$%.2f')为axis添加特殊的格式化程序
  • .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

相关问题 更多 >