Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gallery bokeh #31

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Test_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#MultiGlyph
from bokeh.plotting import figure, show

# prepare some data
x = [1, 2, 3, 4, 5]
y1 = [6, 7, 8, 9, 10]
y2 = [11, 3, 4, 5, 6]
y3 = [4, 5, 15, 7, 2]

# create a new plot with a title and axis labels
p = figure(title="Multiple glyphs example", x_axis_label="x", y_axis_label="y")

# add multiple renderers
p.line(x, y1, legend_label="Temp.", color="blue", line_width=2)
p.vbar(x=x, top=y2, legend_label="Rate", width=0.5, bottom=0, color="red")
p.circle(x, y3, legend_label="Objects", color="yellow", size=12)

# show the results
show(p)
86 changes: 86 additions & 0 deletions histogram.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"from bokeh.layouts import column\n",
"from bokeh.models import Div, TeX\n",
"from bokeh.plotting import figure, show\n",
"\n",
"p = figure(width=670, height=400, toolbar_location=None,\n",
" title=\"Normal (Gaussian) Distribution\")\n",
"\n",
"n = 1000\n",
"rng = np.random.default_rng(825914)\n",
"x = rng.normal(loc=4.7, scale=12.3, size=n)\n",
"\n",
"# Scale random data so that it has mean of 0 and standard deviation of 1\n",
"xbar = x.mean()\n",
"sigma = x.std()\n",
"scaled = (x - xbar) / sigma\n",
"\n",
"# Histogram\n",
"bins = np.linspace(-3, 3, 40)\n",
"hist, edges = np.histogram(scaled, density=True, bins=bins)\n",
"p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:],\n",
" fill_color=\"pink\", line_color=\t\"purple\",\n",
" legend_label=f\"{n} random samples\")\n",
"\n",
"# Probability density function\n",
"x = np.linspace(-3.0, 3.0, 100)\n",
"pdf = np.exp(-0.5*x**2) / np.sqrt(2.0*np.pi)\n",
"p.line(x, pdf, line_width=2, line_color=\"purple\",\n",
" legend_label=\"Probability Density Function\")\n",
"\n",
"p.y_range.start = 0\n",
"p.xaxis.axis_label = \"x\"\n",
"p.yaxis.axis_label = \"PDF(x)\"\n",
"\n",
"p.xaxis.ticker = [-3, -2, -1, 0, 1, 2, 3]\n",
"p.xaxis.major_label_overrides = {\n",
" -3: TeX(r\"\\overline{x} - 3\\sigma\"),\n",
" -2: TeX(r\"\\overline{x} - 2\\sigma\"),\n",
" -1: TeX(r\"\\overline{x} - \\sigma\"),\n",
" 0: TeX(r\"\\overline{x}\"),\n",
" 1: TeX(r\"\\overline{x} + \\sigma\"),\n",
" 2: TeX(r\"\\overline{x} + 2\\sigma\"),\n",
" 3: TeX(r\"\\overline{x} + 3\\sigma\"),\n",
"}\n",
"\n",
"p.yaxis.ticker = [0, 0.1, 0.2, 0.3, 0.4]\n",
"p.yaxis.major_label_overrides = {\n",
" 0: TeX(r\"0\"),\n",
" 0.1: TeX(r\"0.1/\\sigma\"),\n",
" 0.2: TeX(r\"0.2/\\sigma\"),\n",
" 0.3: TeX(r\"0.3/\\sigma\"),\n",
" 0.4: TeX(r\"0.4/\\sigma\"),\n",
"}\n",
"\n",
"div = Div(text=r\"\"\"\n",
"A histogram of a samples from a Normal (Gaussian) distribution, together with\n",
"the ideal probability density function, given by the equation:\n",
"<p />\n",
"$$\n",
"\\qquad PDF(x) = \\frac{1}{\\sigma\\sqrt{2\\pi}} \\exp\\left[-\\frac{1}{2}\n",
"\\left(\\frac{x-\\overline{x}}{\\sigma}\\right)^2 \\right]\n",
"$$\n",
"\"\"\")\n",
"\n",
"show(column(p, div))"
]
}
],
"metadata": {
"language_info": {
"name": "python"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Binary file added histogram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added jitter plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions jitter_plot.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"from bokeh.layouts import column\n",
"from bokeh.plotting import figure, show\n",
"from bokeh.sampledata.autompg import autompg\n",
"from bokeh.transform import jitter,factor_cmap\n",
"from bokeh.io import curdoc\n",
"\n",
"curdoc().theme = 'dark_minimal'\n",
"years = sorted(autompg.yr.unique())\n",
"\n",
"p1 = figure(width=600, height=300, title=\"Years vs mpg without jittering\")\n",
"p1.xgrid.grid_line_color = None\n",
"p1.xaxis.ticker = years\n",
"\n",
"p1.scatter(x='yr', y='mpg', size=9, alpha=0.4,fill_color=\"green\", source=autompg)\n",
"\n",
"p2 = figure(width=600, height=300, title=\"Years vs mpg with jittering\")\n",
"p2.xgrid.grid_line_color = None\n",
"p2.xaxis.ticker = years\n",
"\n",
"p2.scatter(x=jitter('yr', 0.4), y='mpg', size=9, alpha=0.4,fill_color=\"red\", source=autompg)\n",
"\n",
"show(column(p1, p2))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
49 changes: 49 additions & 0 deletions slope.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from bokeh.palettes import Turbo256\n",
"from bokeh.models import Slope\n",
"from bokeh.plotting import figure, show\n",
"from bokeh.transform import linear_cmap\n",
"\n",
"# linear equation parameters\n",
"slope, intercept = 2, 10\n",
"\n",
"x = list(range(0, 8))\n",
"y = [i**2 for i in x]\n",
"# create linear color mapper\n",
"mapper = linear_cmap(field_name=\"y\", palette=Turbo256, low=min(y), high=max(y))\n",
"\n",
"xpts = np.arange(0, 20, 0.2)\n",
"ypts = slope * xpts + intercept + np.random.normal(0, 4, 100)\n",
"\n",
"p = figure(width=450, height=450, x_axis_label='x', y_axis_label='y',\n",
" background_fill_color=\"#fafafa\")\n",
"p.y_range.start = 0\n",
"\n",
"p.circle(xpts, ypts, size=6, alpha=0.6 ,fill_color=mapper)\n",
"\n",
"slope = Slope(gradient=slope, y_intercept=intercept,\n",
" line_color=\"deeppink\", line_dash='dashed', line_width=4)\n",
"\n",
"p.add_layout(slope)\n",
"\n",
"show(p)"
]
}
],
"metadata": {
"language_info": {
"name": "python"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Binary file added slope.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.