Wednesday, July 12, 2017

Basic Bokeh Visualisations



In [70]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
import warnings
warnings.filterwarnings('ignore')
In [96]:
from bokeh.plotting import figure
from bokeh.io import output_file, show, output_notebook
from bokeh.models import Range1d, PanTool, ResetTool, HoverTool
In [72]:
iris = pd.read_csv("iris.csv")
In [73]:
iris.head()
Out[73]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
0 1 5.1 3.5 1.4 0.2 Iris-setosa
1 2 4.9 3.0 1.4 0.2 Iris-setosa
2 3 4.7 3.2 1.3 0.2 Iris-setosa
3 4 4.6 3.1 1.5 0.2 Iris-setosa
4 5 5.0 3.6 1.4 0.2 Iris-setosa
In [74]:
#output_file("scatter.html")
In [97]:
f = figure(tools = [PanTool(), ResetTool(), HoverTool()])
In [76]:
dir(f)
Out[76]:
['__cached_all__overridden_defaults__',
 '__cached_all__properties__',
 '__cached_all__properties_with_refs__',
 '__class__',
 '__container_props__',
 '__dataspecs__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__overridden_defaults__',
 '__properties__',
 '__properties_with_refs__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__subtype__',
 '__view_model__',
 '__weakref__',
 '_attach_document',
 '_axis',
 '_callbacks',
 '_check_colon_in_category_label',
 '_check_missing_renderers',
 '_check_no_data_renderers',
 '_check_required_range',
 '_check_snapped_toolbar_and_axis',
 '_clone',
 '_detach_document',
 '_document',
 '_event_callbacks',
 '_grid',
 '_id',
 '_overridden_defaults',
 '_property_values',
 '_repr_html_',
 '_repr_pretty',
 '_to_json_like',
 '_trigger_event',
 '_unstable_default_values',
 '_unstable_themed_values',
 '_update_event_callbacks',
 'above',
 'add_dynamic_image',
 'add_glyph',
 'add_layout',
 'add_tile',
 'add_tools',
 'annular_wedge',
 'annulus',
 'apply_theme',
 'arc',
 'asterisk',
 'axis',
 'background_fill_alpha',
 'background_fill_color',
 'below',
 'bezier',
 'border_fill_alpha',
 'border_fill_color',
 'circle',
 'circle_cross',
 'circle_x',
 'column',
 'cross',
 'css_classes',
 'dataspecs',
 'dataspecs_with_props',
 'diamond',
 'diamond_cross',
 'disabled',
 'document',
 'ellipse',
 'equals',
 'extra_x_ranges',
 'extra_y_ranges',
 'grid',
 'h_symmetry',
 'hbar',
 'height',
 'hidpi',
 'html',
 'image',
 'image_rgba',
 'image_url',
 'inner_height',
 'inner_width',
 'inverted_triangle',
 'js_event_callbacks',
 'js_on_change',
 'js_on_event',
 'js_property_callbacks',
 'layout',
 'layout_height',
 'layout_width',
 'left',
 'legend',
 'line',
 'lod_factor',
 'lod_interval',
 'lod_threshold',
 'lod_timeout',
 'lookup',
 'min_border',
 'min_border_bottom',
 'min_border_left',
 'min_border_right',
 'min_border_top',
 'multi_line',
 'name',
 'on_change',
 'on_event',
 'outline_line_alpha',
 'outline_line_cap',
 'outline_line_color',
 'outline_line_dash',
 'outline_line_dash_offset',
 'outline_line_join',
 'outline_line_width',
 'oval',
 'patch',
 'patches',
 'plot_height',
 'plot_width',
 'pprint',
 'pretty',
 'properties',
 'properties_containers',
 'properties_with_refs',
 'properties_with_values',
 'quad',
 'quadratic',
 'query_properties_with_values',
 'ray',
 'rect',
 'ref',
 'references',
 'remove_on_change',
 'renderers',
 'right',
 'row',
 'scatter',
 'segment',
 'select',
 'select_one',
 'set_from_json',
 'set_select',
 'sizing_mode',
 'square',
 'square_cross',
 'square_x',
 'subscribed_events',
 'tags',
 'text',
 'themed_values',
 'title',
 'title_location',
 'to_json',
 'to_json_string',
 'tool_events',
 'toolbar',
 'toolbar_location',
 'toolbar_sticky',
 'tools',
 'triangle',
 'trigger',
 'unapply_theme',
 'update',
 'update_from_json',
 'v_symmetry',
 'vbar',
 'webgl',
 'wedge',
 'width',
 'x',
 'x_mapper_type',
 'x_range',
 'xaxis',
 'xgrid',
 'y_mapper_type',
 'y_range',
 'yaxis',
 'ygrid']
In [98]:
output_notebook()
Loading BokehJS ...
In [99]:
colormap = {'Iris-setosa': 'red', 'Iris-versicolor': 'green', 'Iris-virginica': 'blue'}
colors = [colormap[x] for x in iris['Species']]
In [10]:
f.circle(iris.PetalLengthCm, iris.PetalWidthCm, size= 10, fill_alpha = 0.2, color = colors)
Out[10]:
GlyphRenderer(
id = '50332b97-4334-47af-9a9d-15806789704b', …)

change width, height, and background color

In [100]:
f.plot_height = 400
f.plot_width = 600
f.background_fill_color = "grey"
f.background_fill_alpha = 0.4
In [12]:
show(f)

Add title

In [101]:
f.title.text = "Iris Bokeh Demo"
f.title.text_color = "red"
f.title.text_font = "times"
f.title.text_font_size = "44px"

Axes Styling

In [102]:
f.axis.minor_tick_line_color = "red"
f.axis.major_tick_line_color = "blue"
f.xaxis.axis_label = "Petel Length"
f.yaxis.axis_label = "Petal Width"
In [15]:
show(f)

Axis geometry

In [16]:
f.x_range = Range1d(start = 0, end = 10)
f.y_range = Range1d(start = 0, end = 5)
In [17]:
show(f)

Style the tools

In [103]:
f.toolbar.logo = None
In [19]:
show(f)
In [104]:
iris["colors"] = colors
In [84]:
f.circle(iris.PetalLengthCm, iris.PetalWidthCm,  fill_alpha = 0.2, color = colors, size = iris.SepalWidthCm*4)
Out[84]:
GlyphRenderer(
id = 'f7645cb4-895b-4423-8a49-7f027a3cc427', …)
In [85]:
show(f)

Add legend

In [105]:
iris.head()
Out[105]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species colors
0 1 5.1 3.5 1.4 0.2 Iris-setosa red
1 2 4.9 3.0 1.4 0.2 Iris-setosa red
2 3 4.7 3.2 1.3 0.2 Iris-setosa red
3 4 4.6 3.1 1.5 0.2 Iris-setosa red
4 5 5.0 3.6 1.4 0.2 Iris-setosa red
In [106]:
for spec in iris.Species.unique():
    f.circle(iris[iris["Species"] == spec].PetalLengthCm, iris[iris["Species"] == spec].PetalWidthCm,  fill_alpha = 0.2, color = iris[iris["Species"] == spec].colors, size = iris[iris["Species"] == spec].SepalWidthCm*4, legend = spec)
    
    
In [107]:
show(f)

style the legend

In [108]:
f.legend.location = "top_left"
In [109]:
show(f)
In [ ]:
 

No comments :

Post a Comment