Bokeh Tutorial

1.5 Glyphs - Legend

Exercise: Create a legend plot for the temperature map with the Glyph interface

Tips:

  • Glyphs: Text, Rect

In [1]:
# Imports
from bokeh.models.glyphs import Text, Rect
from bokeh.models import Plot, Range1d
from bokeh.palettes import RdBu11
from bokeh.plotting import output_notebook, show

In [2]:
# Output option
output_notebook()


BokehJS successfully loaded.

In [3]:
# Set ranges
xdr = Range1d(0, 100)
ydr = Range1d(0, 500)

In [4]:
# Create plot
plot = Plot(
    x_range=xdr,
    y_range=ydr,
    title="",
    plot_width=100,
    plot_height=500,
    min_border=0,
    toolbar_location=None,
    outline_line_color="#FFFFFF",
)

In [5]:
# For each color in your palette, add a Rect glyph to the plot with the appropriate properties
palette = RdBu11
width = 40
for i, color in enumerate(palette):
    rect = Rect(
        x=40, y=(width * (i + 1)),
        width=width, height=40,
        fill_color=color, line_color='black'
    )
    plot.add_glyph(rect)

In [6]:
# Add text labels and add them to the plot
minimum = Text(x=50, y=0, text=['-6 ºC'])
plot.add_glyph(minimum)
maximum = Text(x=50, y=460, text=['6 ºC'])
plot.add_glyph(maximum)


Out[6]:
<bokeh.models.renderers.GlyphRenderer at 0x1032a8250>

In [7]:
# Show plot
show(plot)