Difference between revisions of "Bokeh: Quick Start"

From OnnoWiki
Jump to navigation Jump to search
Line 28: Line 28:
 
Buat file
 
Buat file
  
  vi /var/www/html/line-chart.py
+
  vi /var/www/html/lines.py
  
 
Isi dengan
 
Isi dengan
Line 48: Line 48:
 
   
 
   
 
  # show the results
 
  # show the results
  show(p)
+
  # show(p)
  
+
Jalankan
 +
 
 +
cd /var/www/html
 +
python /var/www/html/lines.py
 +
 
 +
Akses menggunakan browser
 +
 
 +
http://ip-server/lines.html
  
 
 
 +
Langkah untuk membuat plot menggunakan bokeh.plotting interface adalah:
 
 
 
 
+
* Siapkan data (disini menggunakan python lists).
 
+
* Perintahkan Bokeh untuk membuat output (disini menggunakan output_file(), dengan nama file "lines.html").
When you execute this script, you will see that a new output file "lines.html" is created, and that a browser automaticaly opens a new tab to display it. (For presentation purposes we have included the plot output directly inline in this document.)
+
* Call figure() untuk membuat plot dengan semua opsi, seperti title, tools, label dll.
 
+
* Tambahkan renderers (disini menggunakan, Figure.line) visual dapat di custom seperti color, legend dan width dari plot.
The basic steps to creating plots with the bokeh.plotting interface are:
+
* Perintahkan Bokeh untuk show() atau save() hasilnya.
  
* Prepare some data (in this case plain python lists).
+
Langkah tiga dan empat dapat di ulang untuk membuat lebih dari satu plot. Contoh di bawah ini,
* Tell Bokeh where to generate output (in this case using output_file(), with "lines.html" as the filename to save as).
 
* Call figure() to create a plot with some overall options like title, tools and axes labels.
 
* Add renderers (in this case, Figure.line) for our data, with visual customizations like colors, legends and widths to the plot.
 
* Ask Bokeh to show() or save() the results.
 
 
 
Steps three and four can be repeated to create more than one plot. See some examples of this below.
 
 
 
The bokeh.plotting interface is also quite handy if we need to customize the output a bit more by adding more data series, glyphs, logarithmic axis, etc. It’s also possible to easily combine multiple glyphs together on one plot as shown below:
 
  
 
  from bokeh.plotting import figure, output_file, show
 
  from bokeh.plotting import figure, output_file, show
 
+
 
  # prepare some data
 
  # prepare some data
 
  x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
 
  x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]

Revision as of 14:27, 27 November 2015

Sumber: http://bokeh.pydata.org/en/latest/docs/quickstart.html


Instalasi

Jika kita menggunakan Anaconda, kita dapat menggunakan

conda install bokeh

Ini akan menginstalasi secara lengkap, tanpa pusing.

Jika kita cukup Percaya Diri (PD) bahwa kita mempunyai semua dependensi yang bidiagonal seperti NumPy, Pandas, dan Redis, maka kita dapat menggunakan,

pip install bokeh


Instalasi Web Server

sudo apt-get install apache2 php5 php5-xmlrpc php5-mysql \
php5-gd php5-cli php5-curl mysql-client mysql-server

Contoh Line Chart

Lakukan

cd /var/www/html

Buat file

vi /var/www/html/lines.py

Isi dengan

from bokeh.plotting import figure, output_file, show

# prepare some data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

# output to static HTML file
output_file("lines.html", title="line plot example")

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

# add a line renderer with legend and line thickness
p.line(x, y, legend="Temp.", line_width=2)

# show the results
# show(p)

Jalankan

cd /var/www/html
python /var/www/html/lines.py

Akses menggunakan browser

http://ip-server/lines.html


Langkah untuk membuat plot menggunakan bokeh.plotting interface adalah:


  • Siapkan data (disini menggunakan python lists).
  • Perintahkan Bokeh untuk membuat output (disini menggunakan output_file(), dengan nama file "lines.html").
  • Call figure() untuk membuat plot dengan semua opsi, seperti title, tools, label dll.
  • Tambahkan renderers (disini menggunakan, Figure.line) visual dapat di custom seperti color, legend dan width dari plot.
  • Perintahkan Bokeh untuk show() atau save() hasilnya.

Langkah tiga dan empat dapat di ulang untuk membuat lebih dari satu plot. Contoh di bawah ini,

from bokeh.plotting import figure, output_file, show

# prepare some data
x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
y0 = [i**2 for i in x]
y1 = [10**i for i in x]
y2 = [10**(i**2) for i in x]

# output to static HTML file
output_file("log_lines.html")

# create a new plot
p = figure(
   tools="pan,box_zoom,reset,save",
   y_axis_type="log", y_range=[0.001, 10**11], title="log axis example",
   x_axis_label='sections', y_axis_label='particles'
)

# add some renderers
p.line(x, x, legend="y=x")
p.circle(x, x, legend="y=x", fill_color="white", size=8)
p.line(x, y0, legend="y=x^2", line_width=3)
p.line(x, y1, legend="y=10^x", line_color="red")
p.circle(x, y1, legend="y=10^x", fill_color="red", line_color="red", size=6)
p.line(x, y2, legend="y=10^x^2", line_color="orange", line_dash="4 4")

# show the results
show(p)




Jupyter Notebooks

At this point we should mention Jupyter (formerly IPython) notebooks.

Jupyter notebooks are a fantastic tool for exploratory data analysis, and they are widely used across the “PyData” community. Bokeh integrates seamlessly with Jupyter notebooks. To view the above examples in a notebook, you would only change output_file() to a call to output_notebook() instead.

A large number of static examples may be viewed directly online at the Bokeh NBViewer Gallery.

The Bokeh GitHub repository also has a number of example notebooks in the examples/plotting/notebook/ directory. After cloning the repository, navigate there and run:

ipython notebook

You can open and interact with any of the notebooks listed in the index page that automatically opens up. In particular, you might check out the interact_basic and interact_numba examples that show how Bokeh can be used together with Jupyter interactive widgets.

Referensi