[Visualization] I tried using Bokeh / plotly! 【memorandum】

at first

――This time I touched on Bokeh and plotly, so I would like to leave it in the article as a memorandum. ――Please note that this article is made as a memorandum of your own by referring to various articles. ――Please note that this article is written within the scope of my understanding, and we cannot guarantee the accuracy of the content. (Please let me know if you make a mistake. is).

1. What is Bokeh?

"In the first place, bokeh is the beauty of a blurred area created outside the focal range of the lens, and an expression method that intentionally uses it ([from Wikipedia](https: /) /ja.wikipedia.org/wiki/%E3%83%9C%E3%82%B1_(%E5%86%99%E7%9C%9F))) " That's right (what's this?).

Now, let's get back to the visualization library that can be used with python. I usually use matplotlib / seaborn, but I came up with the idea that there is something that can be expressed a little more dynamically (_ ≒ interactive_). The graphs displayed below are all images and cannot be moved. be careful.

-Official HP

1.1 Installation

According to the official website, you can install either conda or pip.

conda install bokeh or pip install bokeh

1.2 Coding

Well then, I would like to do it. Basically, it is posted in the User Guide on the official website.

-Official User Guide

1.2.1: Library import

from bokeh.plotting import figure, output_file, show

It's like magic. There is no loss to remember.

Now, let's make a graph from the next.

1.2.2: Scatter plot (scatter Markers)

First is the big picture of the code. It looks like the following.

from bokeh.plotting import figure, output_file, show

#1
output_file('scatter.html') 
p = figure(plot_width=400, plot_height=400)

#2
p.circle([1,2,3,4,5], [6,7,2,4,5], size=20, color='navy', alpha=0.5)

#3
show(p)

Then, you will see the graph below. scatterByBokeh (2).png

Let's look at each one.

** # 1: Create html file & set figure ** The output in bokeh seems to be html, not jpeg or png. So this is also like a spell

output_file('xxx.html')

As (at least I) remember to create html as it creates. The following p = figure (plot_width = 400, plot_height = 400) , but I understand that this is ** an operation to create a space to draw a figure **. I understand that this is something like _fig = plt.figure (...) _ in matplotlib.

** # 2: Add the contents of the scatter plot (this time the circle) to p ** The following is p.circle (...) , but I understand this with the image of adding its contents to the space " p " created above. This time I tried to plot with 〇, so I am using . Circle *.

The contents of () are "x-axis", "y-axis", and "display method" from the left.

** # 3: Drawing ** When drawing a diagram, it can be displayed with show (p) (plt.show () in matplotlib).

I think that the basics of bokeh have been suppressed.

1.2.3 Line Graph (Line Graph)

Next, let's make a line graph (the method is almost the same).

from bokeh.plotting import figure, output_file, show

output_file('line.html')
p = figure(plot_width=400, plot_height=400)

p.line([1,2,3,4,5], [6,7,2,4,5], line_width=2)

show(p)

The contents are almost the same as in the scatter plot. The only difference is the "display method" part of p.line (...) , where the line thickness (line_width) is specified. When displayed, it will look like the following.

2020-05-25 (3).png

In this article, I introduced the rudimentary way of writing bokeh, but I hope that everyone can experience the slimy moving figure by moving it with the mouse. Personally, I thought it would be useful for dynamic presentations (I think matplotlib, seaborn is enough for making ppt materials).

2. What is plotly?

It seems that there are free and some paid visualization libraries provided by plotly. Please see the official website below for details.

-Official HP

2.1 Installation

Now let's install plotly. Like bokeh, you can do it with pip or conda.

pip install plotly conda install -c plotly plotly=4.7.1 (from Official Document)

Let's do it! However, it is necessary to devise another way to execute it with jupyter notebook. According to the official HP, in order to use plotly with jupyter notebook

conda install "notebook>=5.3" "ipywidgets>=7.2"

It is necessary to do. In the case of jupyter lab

conda install jupyterlab "ipywidgets=7.5"

It is necessary to do.

2.2: Coding

2.2.1: Import library

import plotly.express as px

First, let's use a library called plotly.express.

2.2.3: Scatter plot

The big picture of the code is below.

import plotly.express as px

x = [1,2,3,4,5]
y = [6,7,2,4,5]

fig = px.scatter(x=x, y=y)
fig.show()

When you do this, newplot.png

A diagram like this is displayed. (Unlike the formula, the data is the same as the one used in bokeh.) It's very simple to use (I thought it was similar to matplotlib).

The difference from Matplotlib and Bokeh is that the axis name is listed even though the axis name (label) is not specified.

I didn't specify the display method in the above, so next I would like to combine it with pandas.DataFrame and try various settings as well.

df = px.data.iris() #Set iris data as data frame

fig = px.scatter(df, x=df.sepal_length, y=df.sepal_width,
                 color=df.species, size=df.petal_length)

fig.show()

Then, the following graph will be output. iris_plotting.png

I throw px.sctter (...) in the fig and make various settings in ().

1 2 3
dataframe Read data df
x= x-axis settings(data+Axis name setting) df.sepal_length
y= y-axis setting(data+Axis name setting) df.sepal_width
color= Color settings(The contents of the set column are reflected) df.species
size= Circle sizing(The contents of the set column are reflected) df.petal_length

First, let's take a quick look at the data of Iris.

df.head()
sepal_length sepal_width petal_length petal_width species species_id
0 5.1 3.5 1.4 0.2 setosa 1
1 4.9 3.0 1.4 0.2 setosa 1
2 4.7 3.2 1.3 0.2 setosa 1
3 4.6 3.1 1.5 0.2 setosa 1
4 5.0 3.6 1.4 0.2 setosa 1

--df: I'm reading a data frame. --x =, y =: x axis, y axis are set. The point to note here is that the data inside is specified and the axis name is also taken as the column name. --color =: Here, we classify using species. --size =: The size of the circle is set. Here, petal_length is used to express the size of the circle.

I think the interesting thing about plotly is that when you touch the data on the graph with the mouse cursor, it shows what is included in the data. I will not deal with it this time, but 3D data is very easy to understand visually.

It works differently from matplotlib and bokeh, so you have to use it to get used to it, but it's very interesting.

Finally

--This time, I touched two visualization libraries. ――Bokeh and plotly are both interesting and versatile because they can be expressed dynamically unlike matplotlib (I think each library has its advantages). ――Personally, I would like to deepen my understanding of plotly (I wonder if it will become a powerful weapon if I become proficient to some extent ...).

that's all.

Recommended Posts

[Visualization] I tried using Bokeh / plotly! 【memorandum】
I tried using parameterized
I tried using argparse
I tried using mimesis
I tried using anytree
I tried using aiomysql
I tried using Summpy
I tried using coturn
I tried using Pipenv
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
I tried using ESPCN
I tried using openpyxl
I tried using Ipython
I tried using PyCaret
I tried using cron
I tried using face_recognition
I tried using Jupyter
I tried using PyCaret
I tried using doctest
I tried using jinja2
I tried using folium
I tried using time-window
[I tried using Pythonista 3] Introduction
I tried using easydict (memo).
I tried face recognition using Face ++
I tried using BigQuery ML
I tried using Amazon Glacier
I tried using git inspector
[Python] I tried using OpenPose
I tried using AWS Chalice
I tried using Slack emojinator
I tried using Tensorboard, a visualization tool for machine learning
I tried using Rotrics Dex Arm # 2
I tried using Thonny (Python / IDE)
I tried server-client communication using tmux
I tried reinforcement learning using PyBrain
I tried deep learning using Theano
Somehow I tried using jupyter notebook
[Kaggle] I tried undersampling using imbalanced-learn
I tried shooting Kamehameha using OpenPose
I tried using the checkio API
[Python] I tried using YOLO v3
I tried asynchronous processing using asyncio
plotly memorandum
I tried using Amazon SQS with django-celery
I tried using Azure Speech to Text.
I tried using Twitter api and Line api
I tried playing a ○ ✕ game using TensorFlow
I tried using Selenium with Headless chrome
I tried drawing a line using turtle
[Kaggle] I tried ensemble learning using LightGBM
I tried the OSS visualization tool, superset
I tried using PyEZ and JSNAPy. Part 2: I tried using PyEZ
I tried using Bayesian Optimization in Python
I tried to classify text using TensorFlow
I tried using Selective search as R-CNN
I tried using pipenv, so a memo
I tried using the BigQuery Storage API
I tried to predict Covid-19 using Darts