def scatterplot(df): s1 = Scatter(df, x='weight', y='height', color='species', marker='species', title='Fish Data', legend='top_right') s2 = Scatter(df, x='weight', y='width', color='species', marker='species', title='Fish Data', legend='top_right') s3 = Scatter(df, x='weight', y='length1', color='species', marker='species', title='Fish Data', legend='top_right') s4 = Scatter(df, x='width', y='height', color='species', marker='species', title='Fish Data', legend='top_right') layout = gridplot([s1, s2], [s3, s4]) output_file('fishdataset.html') show(layout)
def heatmap3(cs3): from bkcharts import HeatMap, show, output_file from bokeh.palettes import RdGy11 as palette # @UnresolvedImport from bokeh.models import HoverTool from bokeh.models import CrosshairTool crosshair = CrosshairTool() hover = HoverTool(tooltips=[ ("index", "$index"), ("(x,y)", "(@x,@y)"), ("score", "@values") ]) print (type(cs3)) score = [] for x in c3.apply(tuple): score.extend(x) data = { 'utterance1': list(cs3.index) * len(cs3.columns), 'utterance2': [item for item in list(cs3.columns) for i in range(len(c3.index))], 'score': score, } output_file('utterance_heatmap.html') hm = HeatMap(data, x='utterance1', y='utterance2', values='score', title='Cosine Similarity', tools=[hover,crosshair], stat=None, palette=palette) #show(hm) return hm
def get_plot_comp(df, player_name): values = blend( 'pass', 'shoot', 'turnover', name='action probability', labels_name='play_action' ) labels = cat(columns='region', sort=False) stacks = cat(columns='play_action', sort=False) colors = color( columns='play_action', palette=['DeepSkyBlue', 'LightGreen', 'LightPink'], sort=False ) title = player_name + ' Action Probability per Region' hover_info = [('action', '@play_action'), ('prob', '@height')] defaults.width = 450 defaults.height = 450 bar = Bar( df, values=values, label=labels, stack=stacks, color=colors, legend='bottom_left', title=title, tooltips=hover_info ) bar.title.text_font_size = '14pt' bar.legend.orientation = 'horizontal' script, div = components(bar) output_file("test_bar.html") return script, div, bar
def boxplot(df): p1 = BoxPlot(df, values='mpg', color='origin', label='origin', title='AutoInfo by region', legend='top_right') p2 = BoxPlot(df, values='hp', color='origin', label='origin', title='AutoInfo by region', legend='top_right') p3 = BoxPlot(df, values='accel', color='origin', label='origin', title='AutoInfo by region', legend='top_right') p4 = BoxPlot(df, values='weight', color='origin', label='origin', title='AutoInfo by region', legend='top_right') layout = gridplot([p1, p2], [p3, p4]) output_file('autodataset.html') show(layout)
def histograms(df): p = Histogram(df, 'sepal length', color='class', label='class', legend='top_right') output_file('irisdataset.html') show(p)
iphover.mode = 'mouse' iphover.line_policy = 'interp' tphover = point_line.select(HoverTool) tphover.mode = 'mouse' shover = scatter.select(HoverTool) shover.mode = 'vline' shoverp = scatter_point.select(HoverTool) shoverp.mode = 'mouse' # set up tooltips int_vhover.tooltips = int_hhover.tooltips = TOOLTIPS tphover.tooltips = iphover.tooltips = TOOLTIPS shover.tooltips = shoverp.tooltips = TOOLTIPS vhover.tooltips = hhover.tooltips = TOOLTIPS output_file("hover_span.html", title="hover_span.py example") show( gridplot(hline, vline, int_hline, int_vline, int_point_line, point_line, scatter_point, scatter, ncols=2))
from bkcharts import Area, show, output_file, defaults from bokeh.layouts import row defaults.width = 400 defaults.height = 400 # create some example data data = dict( python=[2, 3, 7, 5, 26, 221, 44, 233, 254, 265, 266, 267, 120, 111], pypy=[12, 33, 47, 15, 126, 121, 144, 233, 254, 225, 226, 267, 110, 130], jython=[22, 43, 10, 25, 26, 101, 114, 203, 194, 215, 201, 227, 139, 160], ) area1 = Area(data, title="Area Chart", legend="top_left", xlabel='time', ylabel='memory') area2 = Area(data, title="Stacked Area Chart", legend="top_left", stack=True, xlabel='time', ylabel='memory') output_file("area.html", title="area.py example") show(row(area1, area2))
import pandas as pd from bkcharts import Horizon, output_file, show # read in some stock data from the Yahoo Finance API AAPL = pd.read_csv( "http://ichart.yahoo.com/table.csv?s=AAPL&a=0&b=1&c=2000&d=0&e=1&f=2010", parse_dates=['Date']) MSFT = pd.read_csv( "http://ichart.yahoo.com/table.csv?s=MSFT&a=0&b=1&c=2000&d=0&e=1&f=2010", parse_dates=['Date']) IBM = pd.read_csv( "http://ichart.yahoo.com/table.csv?s=IBM&a=0&b=1&c=2000&d=0&e=1&f=2010", parse_dates=['Date']) data = pd.DataFrame(data=dict(AAPL=AAPL['Adj Close'], MSFT=MSFT['Adj Close'], IBM=IBM['Adj Close'], Date=AAPL['Date'])).set_index('Date') hp = Horizon(data, plot_width=800, plot_height=300, title="horizon plot using stock inputs", xlabel='Date') output_file("horizon.html", title="horizon.py example") show(hp)
from bkcharts import BoxPlot, output_file, show from bokeh.sampledata.autompg import autompg as df # origin = the source of the data that makes up the autompg dataset title = "MPG by Cylinders and Data Source, Colored by Cylinders" # color by one dimension and label by two dimensions # coloring by one of the columns visually groups them together box_plot = BoxPlot(df, label=['cyl', 'origin'], values='mpg', color='cyl', title=title) output_file("boxplot_single.html", title="boxplot_single.py example") show(box_plot)
dot_plot8 = Dot(df, label='cyl', values='neg_mpg', agg='mean', group='origin', color='origin', legend='top_right', title="label='cyl' values='neg_mpg' agg='mean' group='origin'") # infer labels from index df = df.set_index('cyl') dot_plot9 = Dot(df, values='mpg', agg='mean', legend='top_right', title='inferred labels') output_file("dots_multi.html", title="dots_multi.py example") show( gridplot(dot_plot, dot_plot2, dot_plot3, dot_plot4, dot_plot5, dot_plot6, dot_plot7, dot_plot8, dot_plot9, ncols=2))
df['date'] = pd.date_range('1/1/2015', periods=len(df.index), freq='D') # default behavior for dataframe input is to plot each numerical column as a line line = Line(df) # build the line plots line0 = Line(df, y=['python', 'pypy', 'jython'], title="Interpreters (y=['python', 'pypy', 'jython'])", ylabel='Duration', legend=True) line1 = Line(df, x='date', y=['python', 'pypy', 'jython'], title="Interpreters (x='date', y=['python', 'pypy', 'jython'])", ylabel='Duration', legend=True) line2 = Line(df, x='date', y=['python', 'pypy', 'jython'], dash=['python', 'pypy', 'jython'], title="Interpreters (x='date', y, dash=['python', 'pypy', 'jython'])", ylabel='Duration', legend=True) line3 = Line(df, x='date', y=['python', 'pypy', 'jython'], dash=['python', 'pypy', 'jython'], color=['python', 'pypy', 'jython'], title="Interpreters (x='date', y, dash, color=['python', 'pypy', 'jython'])", ylabel='Duration', legend=True) line4 = Line(df, x='date', y=['python', 'pypy', 'jython'], dash='test', color=['python', 'pypy', 'jython'], title="Interpreters (x='date', y, color=['python', 'pypy', 'jython'], dash='test') with tooltips", ylabel='Duration', legend=True, tooltips=[('series', '@series'), ('test', '@test')]) output_file("line_multi.html", title="line examples") show(gridplot(line, line0, line1, line2, line3, line4, ncols=2))
from bkcharts import Histogram, output_file, show from bokeh.sampledata.autompg import autompg as df df.sort_values(by='cyl', inplace=True) hist = Histogram(df, values='hp', color='cyl', title="HP Distribution by Cylinder Count", legend='top_right') output_file("histogram_single.html", title="histogram_single.py example") show(hist)
#pip install bokeh=0.12.6 #pip intsall bkcharts #Bokeh User Guide https://bokeh.pydata.org/en/latest/docs/user_guide.html from bkcharts import Scatter, output_file, show import pandas df = pandas.DataFrame(columns=["X","Y"]) df["X"]=[1,3,5,7,9] df["Y"]=[2,4,6,8,0] p = Scatter(df, x="X",y="Y", title="Scatter Test", xlabel="Odd Numbers", ylabel="Even Numbers") # tools="" this will remove tool bar from the graph # tools = "pan, resize" this will show only the listed tools # logo = None this will remove the bokeh logo as well output_file("Scatter_Example.html", mode="relative") # default mode is cdn, we can pass mode ="relative"/"absolute"/"inline" #cdn is content delivery network, the js and css file are donwloaded from intreenet, we can store them locally with relative or absolute. #inline: javascript file and stylesheet will load in line in the html file show(p)
import pandas as pd from bkcharts import output_file, Chord from bokeh.io import show from bokeh.sampledata.les_mis import data nodes = data['nodes'] links = data['links'] nodes_df = pd.DataFrame(nodes) links_df = pd.DataFrame(links) source_data = links_df.merge(nodes_df, how='left', left_on='source', right_index=True) source_data = source_data.merge(nodes_df, how='left', left_on='target', right_index=True) source_data = source_data[source_data["value"] > 5] chord_from_df = Chord(source_data, source="name_x", target="name_y", value="value") output_file('chord_from_df.html', mode="inline") show(chord_from_df)
from bkcharts import Bar, output_file, show from bkcharts.attributes import cat, color from bkcharts.operations import blend from bkcharts.utils import df_from_json from bokeh.sampledata.olympics2014 import data # utilize utility to make it easy to get json/dict data converted to a dataframe df = df_from_json(data) # filter by countries with at least one medal and sort by total medals df = df[df['total'] > 0] df = df.sort_values(by="total", ascending=False) bar = Bar(df, values=blend('bronze', 'silver', 'gold', name='medals', labels_name='medal'), label=cat(columns='abbr', sort=False), stack=cat(columns='medal', sort=False), color=color(columns='medal', palette=['SaddleBrown', 'Silver', 'Goldenrod'], sort=False), legend='top_right', title="Medals per Country, Sorted by Total Medals", tooltips=[('medal', '@medal'), ('country', '@abbr')]) output_file("stacked_bar.html", title="stacked_bar.py example") show(bar)
label='cyl', values='neg_mpg', agg='mean', group='origin', title="label='cyl' values='neg_mpg' agg='mean' group='origin'", legend='top_right') bar_plot8.title.text_font_size = '9pt' # infer labels from index df = df.set_index('cyl') bar_plot9 = Bar(df, values='mpg', agg='mean', legend='top_right', title='inferred labels') bar_plot9.title.text_font_size = '10pt' output_file("bar_multi.html", title="bar_multi.py example") show( gridplot(bar_plot, bar_plot2, bar_plot3, bar_plot4, bar_plot5, bar_plot6, bar_plot7, bar_plot8, bar_plot9, ncols=2))
from bkcharts import Donut, show, output_file from bkcharts.utils import df_from_json from bokeh.sampledata.olympics2014 import data import pandas as pd # utilize utility to make it easy to get json/dict data converted to a dataframe df = df_from_json(data) # filter by countries with at least one medal and sort by total medals df = df[df['total'] > 8] df = df.sort_values(by="total", ascending=False) df = pd.melt(df, id_vars=['abbr'], value_vars=['bronze', 'silver', 'gold'], value_name='medal_count', var_name='medal') # original example d = Donut(df, label=['abbr', 'medal'], values='medal_count', text_font_size='8pt', hover_text='medal_count') output_file("donut.html", title="donut.py example") show(d)
from bkcharts import Histogram, defaults, show, output_file from bokeh.layouts import gridplot from bokeh.sampledata.autompg import autompg as df defaults.plot_width = 400 defaults.plot_height = 350 # input options hist = Histogram(df['mpg'], title="df['mpg']") hist2 = Histogram(df, 'displ', title="df, 'displ'") hist3 = Histogram(df, values='hp', title="df, values='hp'", density=True) hist4 = Histogram(df, values='hp', color='cyl', title="df, values='hp', color='cyl'", legend='top_right') hist5 = Histogram(df, values='mpg', bins=50, title="df, values='mpg', bins=50") hist6 = Histogram(df, values='mpg', bins=[10, 15, 25, 100], tooltips=[('Bin', "@label")], title="df, values='mpg', bins=[10, 15, 25, 100]") output_file("histogram_multi.html", title="histogram_multi.py example") show(gridplot(hist, hist2, hist3, hist4, hist5, hist6, ncols=2))
from bkcharts import Dot, show, output_file # best support is with data in a format that is table-like data = { 'sample': ['1st', '2nd', '1st', '2nd', '1st', '2nd'], 'interpreter': ['python', 'python', 'pypy', 'pypy', 'jython', 'jython'], 'timing': [-2, 5, 12, 40, 22, 30], } # x-axis labels pulled from the interpreter column, stacking labels from sample column dots = Dot(data, values='timing', label='interpreter', group='sample', agg='mean', title="Python Interpreter Sampling", legend='top_right', width=600) output_file("dots.html", title="dots.py example") show(dots)
values='cyl', stat='mean', legend='top_right') hm9 = HeatMap(fruits, y='year', x='fruit', values='fruit_count', stat=None) hm10 = HeatMap(unempl, x='Year', y='Month', values='Unemployment', stat=None, sort_dim={'x': False}, width=900, plot_height=500) output_file("heatmap.html", title="heatmap.py example") show( column( gridplot(hm1, hm2, hm3, hm4, hm5, hm6, hm7, hm8, hm9, ncols=2, plot_width=400, plot_height=400), hm10))
Source: https://en.wikipedia.org/wiki/History_of_United_States_postage_rates """ from bkcharts import Step, show, output_file # build a dataset where multiple columns measure the same thing data = dict( stamp=[ .33, .33, .34, .37, .37, .37, .37, .39, .41, .42, .44, .44, .44, .45, .46, .49, .49 ], postcard=[ .20, .20, .21, .23, .23, .23, .23, .24, .26, .27, .28, .28, .29, .32, .33, .34, .35 ], ) # create a line chart where each column of measures receives a unique color and dash style line = Step(data, y=['stamp', 'postcard'], dash=['stamp', 'postcard'], color=['stamp', 'postcard'], title="U.S. Postage Rates (1999-2015)", ylabel='Rate per ounce', legend=True) output_file("steps.html", title="steps.py example") show(line)
""" This example uses the Iris data to demonstrate the specification of combined variables using chart operations. This specific instance uses a blend, which stacks columns, and renames the combined column. This can be used where the column itself is a type of categorical variable. Here, length and width are derived from the petal and sepal measurements. """ from bkcharts import Scatter, output_file, show from bkcharts.operations import blend from bokeh.sampledata.iris import flowers as data scatter = Scatter(data, x=blend('petal_length', 'sepal_length', name='length'), y=blend('petal_width', 'sepal_width', name='width'), color='species', title='x=petal_length+sepal_length, y=petal_width+sepal_width, color=species', legend='top_right') output_file("iris_blend.html", title="iris_blend.py example") show(scatter)
y='Adj Close', x='Date', title="Timeseries (Tooltips)", tooltips=tooltips) # step tsstep = TimeSeries(data, y=['IBM', 'MSFT', 'AAPL'], legend=True, builder_type='step', title="Timeseries (Step)", tools=TOOLS, ylabel='Stock Prices', xlabel='Date') # point tspoint = TimeSeries(data, y=['IBM', 'MSFT', 'AAPL'], legend=True, builder_type='point', marker=['IBM', 'MSFT', 'AAPL'], color=['IBM', 'MSFT', 'AAPL'], title="Timeseries (Point)", tools=TOOLS, ylabel='Stock Prices', xlabel='Date') output_file("timeseries.html", title="timeseries.py example") show(column(tsline, tsline2, tsline3, tsstep, tspoint))
from bkcharts import Line, show, output_file # build a dataset where multiple columns measure the same thing data = dict( python=[2, 3, 7, 5, 26, 221, 44, 233, 254, 265, 266, 267, 120, 111], pypy=[12, 33, 47, 15, 126, 121, 144, 233, 254, 225, 226, 267, 110, 130], jython=[22, 43, 10, 25, 26, 101, 114, 203, 194, 215, 201, 227, 139, 160], test=[ 'foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'bar' ]) # create a line chart where each column of measures receives a unique color and dash style line = Line(data, y=['python', 'pypy', 'jython'], dash=['python', 'pypy', 'jython'], color=['python', 'pypy', 'jython'], legend_sort_field='color', legend_sort_direction='ascending', title="Interpreter Sample Data", ylabel='Duration', legend=True) output_file("line_single.html", title="line_single.py example") show(line)
from bkcharts import Scatter, output_file, show from bokeh.sampledata.iris import flowers as data scatter = Scatter(data, x='petal_length', y='petal_width', color='species', marker='species', title='Iris Dataset Color and Marker by Species', legend=True) output_file("iris_simple.html", title="iris_simple.py example") show(scatter)
scatter3 = Scatter( df, x='mpg', y='hp', color='origin', title="x='mpg', y='hp', color='origin', with tooltips", xlabel="Miles Per Gallon", ylabel="Horsepower", legend='top_right', tooltips=tooltips) scatter4 = Scatter( df, x='mpg', y='hp', color='cyl', marker='origin', title="x='mpg', y='hp', color='cyl', marker='origin'", xlabel="Miles Per Gallon", ylabel="Horsepower", legend='top_right', tooltips=tooltips) # Example with nested json/dict like data, which has been pre-aggregated and pivoted df2 = df_from_json(data) df2 = df2.sort_values(by='total', ascending=False) df2 = df2.head(10) df2 = pd.melt(df2, id_vars=['abbr', 'name']) scatter5 = Scatter( df2, x='value', y='name', color='variable', title="x='value', y='name', color='variable'", xlabel="Medals", ylabel="Top 10 Countries", legend='bottom_right') scatter6 = Scatter(flowers, x=blend('petal_length', 'sepal_length', name='length'), y=blend('petal_width', 'sepal_width', name='width'), color='species', title='x=petal_length+sepal_length, y=petal_width+sepal_width, color=species', legend='top_right') output_file("scatter_multi.html", title="scatter_multi.py example") show(gridplot(scatter0, scatter2, scatter3, scatter4, scatter5, scatter6, ncols=2))
import numpy as np from bkcharts import Horizon, output_file, show x = np.linspace(0, np.pi * 4, 137) y = (2 * np.random.normal(size=137) + x**2) xx = np.hstack([-1 * x[::-1], x]) yy = np.hstack([-1 * y[::-1], y]) data = dict([('x', xx), ('y', yy), ('y2', yy), ('y3', yy), ('y4', yy), ('y5', yy)]) hp = Horizon(data, x='x', title="test horizon", ylabel='Random') output_file("horizon_folds.html", title="horizon_folds.py example") show(hp)
title="label='cyl', values='mpg', marker='cross'") # color whisker by cylinder box_plot7 = BoxPlot(df, label='cyl', values='mpg', whisker_color='cyl', title="label='cyl', values='mpg', whisker_color='cyl'") # remove outliers box_plot8 = BoxPlot( df, label='cyl', values='mpg', outliers=False, title="label='cyl', values='mpg', outliers=False, tooltips=True", tooltips=True) output_file("boxplot_multi.html", title="boxplot_multi.py example") show( gridplot(box_plot, box_plot2, box_plot3, box_plot4, box_plot5, box_plot6, box_plot7, box_plot8, ncols=2))