<jupyter_start><jupyter_text>Bokeh Bokeh is an interactive visualization library that targets modern web browsers for presentation. Bokeh is a tool in the Charting Libraries category of a tech stack. It is good for:- Interactive visualization- Standalone HTML documents, or server-backed apps- Expressive and versatile graphics- Large, dynamic or streaming data- Easy usage from python (or Scala, or R, or...) Curently has two main interfaces:  - bokeh.models -A low-level interface that provides the most flexibility to application develpers.  - bokeh.ploting - A higher-lever interface centered around composing visual glyphs.  for instalation -> python package index at python.org and see __installation__ instructions for bokeh. Alternative to Bokeh:   - Plotly   - Matplotlib   - Tableau etc According to the StackShare community, Matplotlib has a broader approval, being mentioned in 10 company stacks & 19 developers stacks; compared to Bokeh, which is listed in 4 company stacks and 7 developer stacks.- __Matplotlib__ is based on the plotting capabilities of MATLAB, the scientific programming, analysis and plotting environment and programming language. It __does not__ provide a way to generate interactive plots that can be viewed in the web browser.when it comes to Tableau, it's like comparing apple and oranges:- __Tableau__ can help anyone see and understand their data. Connect to almost any database, drag and drop to create visualizations, and share with a click.     - from personal experince i would say troubleshooting is hard when integrating with databases, not sure if it was because i was using a free version. But still i couldn't find enough resource.- __Bokeh__ - goal is to provide elegant, concise construction of __versatile graphics__, and to extend this capability with __high-performance__ interactivity over very large or streaming datasets. It can help anyone who would like to quickly and easily create interactive plots, dashboards, and data applications.  - It's not easy to learn each new function and takes time.  - provides the most important types of graph and they are all provided in line with the modern HTML 5 web standard, which has speed advantage. But, some browser do not support the HTML5 standard. As of photograpy -->Bokeh, also known as “Boke” is one of the most popular subjects in photography. The reason why it is so popular, is because Bokeh makes photographs visually appealing, forcing us to focus our attention on a particular area of the image. __to run from the terminal__bokeh serve --show filename.ipynb command __ColumnDataSource__  is the core of most Bokeh plots, providing the data that is visualized by the glyphs of the plot. With the ColumnDataSource , it is easy to share data between multiple plots and widgets, such as the DataTable . dataset -> https://fivethirtyeight.com/features/dear-mona-followup-where-do-people-drink-the-most-beer-wine-and-spirits/WHO presents the data in liters of pure alcohol consumed in 2010. But to make the comparisons more comprehensible (after all, when was the last time you ordered a liter of pure alcohol?), I’ve taken average alcohol content and average serving size for each beverage and converted those numbers into standard serving sizes. The results show how many glasses of wine, cans of beer and shots of spirits were drunk per person in each country in 2010. Here are the countries that drink the most of each drink in per-capita terms (you can find the full results in the lengthy table at the bottom of this article):refer dataset link provided above.<jupyter_code>import pandas as pd
df_drinks = pd.read_csv("/Users/mtessema/Desktop/PY/drinks.csv", skipinitialspace=True, engine='python')
df_drinks<jupyter_output><empty_output><jupyter_text>__=>__ I have taken the head and tail of the data set to do examples.<jupyter_code>from bokeh.io import output_notebook, show
output_notebook()
import numpy as np 
from bokeh.plotting import figure
from bokeh.io import output_file, show
from bokeh.models.tools import HoverTool
import psycopg2
from sqlalchemy import create_engine
# postgres db connection
engine = create_engine('postgresql://postgres:zipcoder@localhost/airflow_test')
# created engine
df_pi=pd.read_sql('select * from drinkers where total_litres_of_pure_alcohol >0.0 and total_litres_of_pure_alcohol < 1.0 order by "total_litres_of_pure_alcohol" ASC;', engine)
# otal_litres_of_pure_alcohol b/n 0.0 and 1.0
pidict = dict(zip(df_pi.country, df_pi.total_litres_of_pure_alcohol))
# created dictionary for the pie chart(data source)
from math import pi
from bokeh.palettes import Category20c, viridis
from bokeh.transform import cumsum

output_file("pie.html")

x = {
 'Timor-Leste': 0.1,
 'Saudi Arabia': 0.1,
 'Indonesia': 0.1,
 'Myanmar': 0.1,
 'Comoros': 0.1,
 'Yemen': 0.1,
 'Niger': 0.1,
 'Iraq': 0.2,
 'Egypt': 0.2,
 'Nepal': 0.2,
 'Guinea': 0.2,
 'Malaysia': 0.3,
 'Senegal': 0.3,
 'Tajikistan': 0.3,
 'Bhutan': 0.4,
 'Chad': 0.4,
 'Morocco': 0.5,
 'Eritrea': 0.5,
 'Jordan': 0.5,
 'Brunei': 0.6,
 'Mali': 0.6,
 'Algeria': 0.7,
 'Ethiopia': 0.7,
 'Oman': 0.7,
 'Madagascar': 0.8,
 'Vanuatu': 0.9,
 'Qatar': 0.9}

data = pd.Series(x).reset_index(name='value').rename(columns={'index':'country'})
data['angle'] = data['value']/data['value'].sum() * 2*pi
data['color'] = viridis(28)[len(x)]

p = figure(plot_height=550, title="Pie Chart", toolbar_location=None,
           tools="hover", tooltips="@country: @value", x_range=(-0.5, 0.5))

p.wedge(x=0, y=1, radius=0.4,
        start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),
                fill_color='color', 
        source=data)
p.axis.axis_label=None
p.axis.visible=False
p.grid.grid_line_color = None
hover = HoverTool()


p.add_tools(hover)
show(p)
df_sec=pd.read_sql('select * from drinkers where total_litres_of_pure_alcohol >= 6.5 and total_litres_of_pure_alcohol < 10.0 order by "total_litres_of_pure_alcohol" ASC;', engine)
#df_sec
df_sec.head(2)
from bokeh.models import ColumnDataSource
from bokeh.transform import factor_cmap
from bokeh.palettes import Category20c, viridis

source = ColumnDataSource(df_sec)
output_file('index.html')
country_list = source.data['country'].tolist()

p = figure(
    y_range=country_list,
    plot_width=600,
    plot_height=600,
    title='total alcohol consumption per country(6.5-10)',
    x_axis_label ='total_litres_of_pure_alcohol',
    tools = 'pan, box_select, zoom_in, zoom_out, save, reset')
# Render glpyh
p.hbar(
    y = 'country',
    right = 'total_litres_of_pure_alcohol', 
    left = 0,
    height = 0.2, 
    #color = 'orange',
        fill_color = factor_cmap(
        'country',
    palette = viridis(38),
    factors = country_list
    
    ),
    
    fill_alpha = 0.9,
    source = source,
    legend = 'country'
)
# add legend
p.legend.orientation = 'vertical'
p.legend.location = 'bottom_right'
p.legend.label_text_font_size = '6px'
# add tooltips(hoover)
hover = HoverTool()
hover.tooltips = """
    <div>
        <h3>@country</h3>
        <div><strong>tot: </strong>@total_litres_of_pure_alcohol</div>
        <div><strong>spirit_servings: </strong>@spirit_servings</div>
        <div><strong>beer_servings: </strong>@beer_servings</div>
        <div><strong>wine_servings: </strong>@wine_servings</div>
    </div>
"""
p.add_tools(hover)

show(p)
from bokeh.plotting import figure, output_file, show
from bokeh.tile_providers import CARTODBPOSITRON, get_provider

output_file("tile.html")

tile_provider = get_provider(CARTODBPOSITRON)

# range bounds supplied in web mercator coordinates
p = figure(x_range=(-2000000, 6000000), y_range=(-1000000, 7000000),
           x_axis_type="mercator", y_axis_type="mercator",
          tools='pan, box_select, zoom_in, zoom_out, save, reset',
           frame_width=600,
           frame_height=600,)

p.add_tile(tile_provider)

show(p)
output_notebook()
output_notebook()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09587485807997982, 'fraction_numerical': 0.01980572726125899, 'mean_word_length': 4.614730878186969, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 35, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '477704', 'license': 'MIT', 'n_tokens_mistral': 2636, 'n_tokens_neox': 2462, 'n_words': 865, 'path': 'Bokeh.ipynb', 'repo_name': 'malbt/Week9-ResearchProjects', 'size': '7927'}