――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).
"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.
According to the official website, you can install either conda
or pip
.
conda install bokeh
or
pip install bokeh
Well then, I would like to do it. Basically, it is posted in the User Guide on the official website.
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.
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.
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.
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.
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).
It seems that there are free and some paid visualization libraries provided by plotly. Please see the official website below for details.
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.
import plotly.express as px
First, let's use a library called plotly.express.
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,
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.
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.
--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