Как нарисовать круг с помощью LinearColorMapper с помощью Python Bokeh - PullRequest
0 голосов
/ 25 апреля 2018

С помощью следующего кода:

from bokeh.plotting import figure, show, output_file
from bokeh.sampledata.iris import flowers

colormap = {'setosa': 'red', 'versicolor': 'green', 'virginica': 'blue'}
colors = [colormap[x] for x in flowers['species']]

p = figure(title = "Iris Morphology")
p.xaxis.axis_label = 'Petal Length'
p.yaxis.axis_label = 'Petal Width'

p.circle(flowers["petal_length"], flowers["petal_width"],
         color=colors, fill_alpha=0.2, size=10)

output_file("iris.html", title="iris.py example")

show(p)

Я могу сделать круговую диаграмму, на которой я раскрасить виды:

enter image description here

Но я хочу покрасить все точки в зависимости от диапазона значений в petal_length.

Я попробовал этот код, но не получилось:

from bokeh.models import LinearColorMapper
exp_cmap = LinearColorMapper(palette='Viridis256', low = min(flowers["petal_length"]), high = max(flowers["petal_length"]))

p.circle(flowers["petal_length"], flowers["petal_width"], 
         fill_color = {'field'  : flowers["petal_lengh"], 'transform' : exp_cmap})

output_file("iris.html", title="iris.py example")

show(p)

А также в окончательном желаемом графике, как я могу поместить цветную полосу, которая показывает диапазон значений и назначенное значение.Примерно так:

enter image description here

Я использую Python 2.7.13.

Ответы [ 2 ]

0 голосов
/ 26 апреля 2018

Преобразователь colormapper ссылается на имя столбца и не принимает фактические литеральные списки данных.Таким образом, все данные должны быть в Bokeh ColumDataSource, а все функции построения графиков должны ссылаться на имена столбцов.К счастью, это просто:

p.circle("petal_length", "petal_width", source=flowers, size=20,
         fill_color = {'field': 'petal_length', 'transform': exp_cmap})

enter image description here

Указания к легендам за пределами участка описаны здесь:

https://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#outside-the-plot-area

0 голосов
/ 26 апреля 2018

Чтобы ответить на вашу первую часть, была небольшая опечатка (petal_lengh вместо petal_length), но что более важно, использование bokeh.ColumnDataSource решит вашу проблему (я пытался сделать это без CDS и получил только ошибки столбца):

from bokeh.plotting import figure, show, output_file
from bokeh.sampledata.iris import flowers  
from bokeh.models import LinearColorMapper
from bokeh.models import ColumnDataSource

p = figure(title = "Iris Morphology")
p.xaxis.axis_label = "Petal Length"
p.yaxis.axis_label = "Petal Width"

source = ColumnDataSource(flowers)

exp_cmap = LinearColorMapper(palette="Viridis256", 
                             low = min(flowers["petal_length"]), 
                             high = max(flowers["petal_length"]))

p.circle("petal_length", "petal_width", source=source, line_color=None,
        fill_color={"field":"petal_length", "transform":exp_cmap})

# ANSWER SECOND PART - COLORBAR
# To display a color bar you'll need to import 
# the `bokeh.models.ColorBar` class and pass it your mapper.
from bokeh.models import ColorBar
bar = ColorBar(color_mapper=exp_cmap, location=(0,0))
p.add_layout(bar, "left")

show(p)

enter image description here

Смотри также: https://github.com/bokeh/bokeh/blob/master/examples/plotting/file/color_data_map.py

...