简体   繁体   中英

Change label ordering in bokeh heatmap

From the bokeh examples

from bokeh.charts import HeatMap, output_file, show

data = {'fruit': ['apples']*3 + ['bananas']*3 + ['pears']*3,
        'fruit_count': [4, 5, 8, 1, 2, 4, 6, 5, 4],
        'sample': [1, 2, 3]*3}

hm = HeatMap(data, x='fruit', y='sample', values='fruit_count',
             title='Fruits', stat=None)

show(hm)

is there a workaround for changing the order in which the labels are displayed? For example, if I wanted to show pears first?

First, you should not use bokeh.charts . It was deprecated, and has been removed from core Bokeh to a separate bkcharts repo. It is completely unsupported and unmaintained . It will not see any new features, bugfixes, improvements, or documentation. It is a dead end.


There are two good options to create this chart:

1) Use the stable and well-supported bokeh.plotting API. This is slightly more verbose, but gives you explicit control over everything, eg the order if the categories. In the code below these are specified as x_range and y_range values to figure :

from bokeh.io import output_file, show
from bokeh.models import ColumnDataSource, LinearColorMapper
from bokeh.palettes import Spectral9
from bokeh.plotting import figure
from bokeh.transform import transform

source = ColumnDataSource(data={
    'fruit': ['apples']*3 + ['bananas']*3 + ['pears']*3,
    'fruit_count': [4, 5, 8, 1, 2, 4, 6, 5, 4],
    'sample': ['1', '2', '3']*3,
})

mapper = LinearColorMapper(palette=Spectral9, low=0, high=8)

p = figure(x_range=['apples', 'bananas', 'pears'], y_range=['1', '2', '3'], 
           title='Fruits')
p.rect(x='fruit', y='sample', width=1, height=1, line_color=None,
       fill_color=transform('fruit_count', mapper), source=source)

show(p)

This yields the output below:

在此处输入图片说明

You can find much more information (as well as live examples) about categorical data with Bokeh in the Handling Categorical Data sections of the User's Guide.

2) Look into HoloViews , which is a very high level API on top of Bokeh that is actively maintained by a team, and endorsed by the Bokeh team as well. A simple HeatMap in HoloViews is typically a one-liner as with bokeh.charts .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM