Source code for bokeh.models.renderers.contour_renderer
#-----------------------------------------------------------------------------# Copyright (c) 2012 - 2023, Anaconda, Inc., and Bokeh Contributors.# All rights reserved.## The full license is in the file LICENSE.txt, distributed with this software.#-----------------------------------------------------------------------------''' Renderer for contour lines and filled polygons.'''#-----------------------------------------------------------------------------# Boilerplate#-----------------------------------------------------------------------------from__future__importannotationsimportlogging# isort:skiplog=logging.getLogger(__name__)#-----------------------------------------------------------------------------# Imports#-----------------------------------------------------------------------------# Standard library importsfromtypingimportTYPE_CHECKING# Bokeh importsfrom...core.propertiesimportFloat,Instance,Seqfrom.glyph_rendererimportGlyphRendererfrom.rendererimportDataRendererifTYPE_CHECKING:from...plotting.contourimportContourDatafrom..annotationsimportContourColorBar#-----------------------------------------------------------------------------# Globals and constants#-----------------------------------------------------------------------------__all__=('ContourRenderer',)#-----------------------------------------------------------------------------# General API#-----------------------------------------------------------------------------
[docs]classContourRenderer(DataRenderer):''' Renderer for contour plots composed of filled polygons and/or lines. Rather than create these manually it is usually better to use :func:`~bokeh.plotting.figure.contour` instead. '''# explicit __init__ to support Init signaturesdef__init__(self,*args,**kwargs)->None:super().__init__(*args,**kwargs)line_renderer=Instance(GlyphRenderer,help=""" Glyph renderer used for contour lines. """)fill_renderer=Instance(GlyphRenderer,help=""" Glyph renderer used for filled contour polygons. """)levels=Seq(Float,default=[],help=""" Levels at which the contours are calculated. """)
[docs]defset_data(self,data:ContourData)->None:''' Set the contour line and filled polygon data to render. Accepts a :class:`~bokeh.plotting.contour.ContourData` object, such as is returned from :func:`~bokeh.plotting.contour.contour_data`. '''ifdata.fill_data:# Convert dataclass to dict to add new fields and put into CDS.fill_data=data.fill_data.asdict()# Copy fill and hatch properties from old to new data sourceold_fill_data=self.fill_renderer.data_source.datafornameinold_fill_data.keys():ifnamenotin("xs","ys","lower_levels","upper_levels"):fill_data[name]=old_fill_data[name]self.fill_renderer.data_source.data=fill_dataelse:self.fill_renderer.data_source.data=dict(xs=[],ys=[],lower_levels=[],upper_levels=[])ifdata.line_data:# Convert dataclass to dict to add new fields and put into CDS.line_data=data.line_data.asdict()# Copy line properties from old to new data sourceold_line_data=self.line_renderer.data_source.datafornameinold_line_data.keys():ifnamenotin("xs","ys","levels"):line_data[name]=old_line_data[name]self.line_renderer.data_source.data=line_dataelse:self.line_renderer.data_source.data=dict(xs=[],ys=[],levels=[])
[docs]defconstruct_color_bar(self,**kwargs)->ContourColorBar:''' Construct and return a new ``ContourColorBar`` for this ``ContourRenderer``. The color bar will use the same fill, hatch and line visual properties as the ContourRenderer. Extra keyword arguments may be passed in to control ``BaseColorBar`` properties such as `title`. '''from..annotationsimportContourColorBarfrom..tickersimportFixedTickerreturnContourColorBar(fill_renderer=self.fill_renderer,line_renderer=self.line_renderer,levels=self.levels,ticker=FixedTicker(ticks=self.levels),**kwargs,)
#-----------------------------------------------------------------------------# Dev API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Private API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Code#-----------------------------------------------------------------------------