Box plots

The box plot is usually informative and also helpful, especially when you have too much to show with very less data. Let's look at an example. Create a script called plotly_box_plot.py and write the following content in it:

import random
import plotly
from numpy import *

N = 50.
c = ['hsl('+str(h)+',50%'+',50%)' for h in linspace(0, 360, N)]
data_set = [{
'y': 3.5*sin(pi * i/N) + i/N+(1.5+0.5*cos(pi*i/N))*random.rand(20),
'type':'box',
'marker':{'color': c[i]}
} for i in range(int(N))]

layout = {'xaxis': {'showgrid':False,'zeroline':False, 'tickangle':45,'showticklabels':False},
'yaxis': {'zeroline':False,'gridcolor':'white'},
'paper_bgcolor': 'rgb(233,233,233)',
'plot_bgcolor': 'rgb(233,233,233)',
}

plotly.offline.plot(data_set)

Run the script and you will get the following output:

student@ubuntu:~/work$ python3 plotly_box_plot.py

The output is as follows:

In the preceding example, we imported plotly, as well as the numpy module. Then we declared N as the total number boxes in the box plot and generated an array of rainbow colors by fixing the saturation and lightness of the HSL representation of color and marching around the hue. Each box is represented by a dictionary that contains the data, the type, and the color. We use list comprehension to describe N boxes, each with a different color and with different randomly generated data. After that, we format the layout of the output and plot the data through the offline plotly function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset