Bokeh Tutorial

1.5 Glyphs - Legend

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

Tips:

  • Glyphs: Text, Rect

In [ ]:
# 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 [ ]:
# Output option
output_notebook()

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

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

In [ ]:
# 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 [ ]:
# Add text labels and add them to the plot
minimum = Text(x=40, y=0, text=['-6 ºC'])
plot.add_glyph(minimum)
maximum = Text(x=40, y=460, text=['6 ºC'])
plot.add_glyph(maximum)

In [ ]:
# Show plot
show(plot)

In [ ]:


In [ ]: