<jupyter_start><jupyter_text>Plotting in Bokeh Over the last few weeks, we've spend a lot of time analyzing our text data and meticulously pulling out important information. Even though our spreadsheets and notebook tables are useful, our data can best be showcased using cool visualizations. The Bokeh library in Python allows us to create powerful visualizations of our data quite easily. Bokeh vs. GephiAs it turns out, not everyone's data can be visualizated in its fullest potential in a network! In certain data sets where the classic network-style connections do not exist, it is better to visualize points using other means. Bokeh gives us the ability to do so. Feel free to discuss with Adam if you are unsure which program is best suited for your data set!<jupyter_code>!pip install bokeh 
import numpy as np
import pandas as pd
from datascience import *
from IPython.core.display import display, HTML
import codecs

from bokeh.plotting import figure, show, output_file
from bokeh.palettes import brewer
from bokeh.charts import Scatter, output_file, show
from bokeh.charts import Bar, output_file, show<jupyter_output><empty_output><jupyter_text>Uploading CSV's ![](google_sheets_coffee.png)<jupyter_code># We are reading the CSV that contains Clayton's data and selecting a range of points
coffee = Table().read_table('CTCPcc.csv')
coffee = coffee.take(range(6, 111))
coffee
# In this cell, we are using the "relabeled" function to rename the columns into names that are more friendly/relevant
original_names = coffee.labels
new_names = ['Year', 'Population (millions)', 'Instant (GBE)', 'Instant (Retail)', 'Regular (GBE)', 'Regular (Retail)', 'Total (GBE)', 'Total (Retail)', 'Tea (DLE)', 'Cocoa (BE)', 'Cocoa (CLE)']
coffee = coffee.relabeled(original_names, new_names)
c2 = coffee.to_df()
coffee<jupyter_output><empty_output><jupyter_text>Time SeriesIn this section, we will be plotting a line graph of the population vs. time. Documentation on specific Bokeh functions is available on the site: http://bokeh.pydata.org/en/latest/docs/user_guide.htmluserguideTry to parse through the code and see what variables you can change around to modify the input data/labels.<jupyter_code>def datetime(x):
    return np.array(x, dtype=np.datetime64)

a = figure(x_axis_type="datetime", title="Population vs Year")
a.grid.grid_line_alpha=0.3
a.xaxis.axis_label = 'Year'
a.yaxis.axis_label = 'Population'

a.line(datetime(c2['Year']), c2['Population (millions)'], color='#A6CEE3', legend='Population')
a.legend.location = "top_left"

show(a)<jupyter_output><empty_output><jupyter_text>Time Series of Population Growth Rates<jupyter_code>#In this cell, we are creating a table that helps us calculate the percent change from year to year
c3 = c2.copy()
c3 = c3[['Year', 'Population (millions)']]
c3 = c3.set_index('Year')
c3['Population (millions)'] = c3['Population (millions)'].apply(pd.to_numeric)
c3['Population (millions)'] = c3.pct_change(1)['Population (millions)']
c3

# plotting the growth rate
pop_growth_plot = figure(x_axis_type="datetime", title="Population vs Year")
pop_growth_plot.grid.grid_line_alpha=0.3
pop_growth_plot.xaxis.axis_label = 'Year'
pop_growth_plot.yaxis.axis_label = 'Population (percent change)'

pop_growth_plot.line(datetime(c2['Year']), c3['Population (millions)'], color='#A6CEE3', legend='Population')
pop_growth_plot.legend.location = "top_left"

show(pop_growth_plot)<jupyter_output><empty_output><jupyter_text>Time Series of Multiple 5 Year Growth RatesFor this visualization, instead of just having one line, we are going to plot multiple lines and compare them. We will be using very similar methods to before to obtain the table. This time however, we will plot the growth rate percents all together so we can compare them visually.<jupyter_code># Calculating 5 year growth rates
c4 = c2.copy()
c4 = c4[['Population (millions)', 'Instant (GBE)', 'Regular (GBE)', 'Total (GBE)', 'Cocoa (CLE)']]
c4['Instant (GBE)'] = [0 if x == 'nan'  else x for x in c4['Instant (GBE)']]
c4 = c4.apply(pd.to_numeric).pct_change(5)
c4
# Plotting the 5 Year Growth rates in one graph from the table above
pop_growth_plot = figure(x_axis_type="datetime", title="Growth Rates over Time")
pop_growth_plot.grid.grid_line_alpha=0.3
pop_growth_plot.xaxis.axis_label = 'Year'
pop_growth_plot.yaxis.axis_label = 'Percent Change (in decimal)'

pop_growth_plot.line(datetime(c2['Year']), c4['Population (millions)'], color='#A6CEE3', legend='Population')
pop_growth_plot.line(datetime(c2['Year']), c4['Instant (GBE)'], color='#B2DF8A', legend='Instant')
pop_growth_plot.line(datetime(c2['Year']), c4['Regular (GBE)'], color='#33A02C', legend='Regular')
pop_growth_plot.line(datetime(c2['Year']), c4['Total (GBE)'], color='#FB9A99', legend='Total Coffee')
pop_growth_plot.legend.location = "top_left"

show(pop_growth_plot)<jupyter_output><empty_output><jupyter_text>Other Types of Visualizations  Scatter<jupyter_code>#This will draw a scatter plot of the Population and Cocoa (CLE) colums from c4
#The first argument is the table from which you are pulling, and the x and y allow you to specify which columsn you want
p = Scatter(c4, x='Population (millions)', y='Cocoa (CLE)',
            title="Scatter - Cocoa (CLE) vs Population (millions)", legend="top_left",
            xlabel="Population (millions)", ylabel="Cocoa (CLE)")
show(p)<jupyter_output><empty_output><jupyter_text>Scatter plots in Bokeh give you many possible features and options to play around with; check out all the options at http://bokeh.pydata.org/en/latest/docs/user_guide/charts.htmlscatter-plots  Bar<jupyter_code>c5 = c2.copy()
c5 = c5[['Cocoa (CLE)']]
c5 = c5.apply(pd.to_numeric)
p = Bar(c5, 'Cocoa (CLE)', values='Cocoa (CLE)', title="Bar - Cocoa", legend="")
show(p)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11812381445076738, 'fraction_numerical': 0.012933264355923435, 'mean_word_length': 6.388535031847134, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '140428', 'license': 'MIT', 'n_tokens_mistral': 1991, 'n_tokens_neox': 1878, 'n_words': 608, 'path': 'Visualization/Bokeh Time Series.ipynb', 'repo_name': 'ds-modules/NESTUD-190A', 'size': '5799'}