The legend of a graph reflects the data displayed in the graph’s Y-axis. In Bokeh, the legends correspond to glyphs. This article how Legends appearing in the bokeh plot can be customized.
We can customize the legend with its several attributes such as location, color, line-color, font-size, font-style line width. We can modify it using the legend attribute with the required property name.
Syntax : legend.property_name=value
Parameter:
Here property name can take the following values
- location
- title
- label_text_font
- label_text_font_style
- label_text_color
- border_line_width
- border_line_color
- border_line_alpha
- background_fill_color
- background_fill_alpha
Approach
- Import module
- Draw a normal plot
- Customize accordingly
- Display plot
Example 1:
Python3
# import module from bokeh.plotting import figure, show # create data currentList = [ 1 , 2 , 3 , 4 , 5 ] List1 = [i * 2 for i in currentList] List2 = [i + 2 for i in currentList] # plot data plots = figure(title = " your Legend Customization" ) line = plots.line( currentList, List1, legend_label = "Arrays ." , line_color = "blue" , line_width = 2 ) circle = plots.circle( currentList, List2, legend_label = "List" , fill_color = "black" , fill_alpha = 0.4 , line_color = "blue" , size = 30 , ) # display legend in top right corner plots.legend.location = "top_right" # give title to legend plots.legend.title = "Your observations" # customize legend appearance plots.legend.label_text_font = "times" plots.legend.label_text_font_style = "italic" plots.legend.label_text_color = "red" # customize border and background of legend plots.legend.border_line_width = 15 plots.legend.border_line_color = "pink" plots.legend.border_line_alpha = 0.5 plots.legend.background_fill_color = "orange" plots.legend.background_fill_alpha = 0.3 # display plot show(plots) |
Output :
Example 2 :
Python3
# import module from bokeh.plotting import figure, show # create data currentList = [ 1 , 2 , 3 , 4 , 5 ] List1 = [i * 2 for i in currentList] List2 = [i + 2 for i in currentList] # plot data plots = figure(title = "Legend Customization" ) line = plots.line(currentList, List1, legend_label = "Arrays ." , line_color = "blue" , line_width = 2 ) circle = plots.circle( currentList, List2, legend_label = "List" , fill_color = "black" , fill_alpha = 0.5 , line_color = "blue" , size = 40 , ) # display legend in top left corner plots.legend.location = "top_left" #give title to legend plots.legend.title = "Observation of plot" #customize legend appearance plots.legend.label_text_font = "times" plots.legend.label_text_font_style = "bold" plots.legend.label_text_color = "black" # customize border and background of legend plots.legend.border_line_width = 9 plots.legend.border_line_color = "green" plots.legend.border_line_alpha = 0.7 plots.legend.background_fill_color = "magenta" plots.legend.background_fill_alpha = 0.2 show(plots) |
Output :
Example 3 :
Python3
# import module from bokeh.plotting import figure, show # create data currentList = [ 1 , 2 , 3 , 4 , 5 ] List1 = [i * 2 for i in currentList] List2 = [i + 2 for i in currentList] # plot data plots = figure(title = "Legend Customization" ) line = plots.line(currentList, List1, legend_label = "Arrays ." , line_color = "blue" , line_width = 2 ) circle = plots.circle( currentList, List2, legend_label = "List" , fill_color = "black" , fill_alpha = 0.5 , line_color = "blue" , size = 50 , ) # display legend in top right corner plots.legend.location = "top_center" # give title to legend plots.legend.title = " Your Observations " # customize legend appearance plots.legend.label_text_font = "times" plots.legend.label_text_font_style = "normal" plots.legend.label_text_color = "red" # customize border and background of legend plots.legend.border_line_width = 3 plots.legend.border_line_color = "grey" plots.legend.border_line_alpha = 0.8 plots.legend.background_fill_color = "orange" plots.legend.background_fill_alpha = 0.2 show(plots) |
Output