Multi-line plots using python bokeh.pallettes
I am trying to plot a line chart for the length of a list "w" in the below code. when i use spectral11 from bokeh all i get is just 11 lines in a diagram where the list contains 24 parameters. are there any other palettes that allow me to display all lines from the list "w"
#Import the library
import pandas
import bokeh
import MySQLdb
from bokeh.plotting import figure, output_file, show
from bokeh.palettes import Spectral11
w=['F1','F2','F3','F4','F5','F6','F7','F8','F9','F10','F11','F12','G1','G2','G3','G4','G5','G6','G7','G8','G9','G10','G11','G12']
p = figure(plot_width=800, plot_height=500, x_axis_type="datetime")
p.title.text = 'Click on legend entries to hide the corresponding lines'
# Open database connection
db = MySQLdb.connect("localhost","user","password","db" )
The loop below generates only 11 data frames, which ultimately display those 11 lines.
for name, color in zip(w, Spectral11):
stmnt='select date_time,col1,w,test_value from db where w="%s"'%(name)
df=pandas.read_sql(stmnt,con=db)
p.line(df['date_time'], df['test_value'], line_width=2, color=color, alpha=0.8, legend=name)
p.legend.location = "top_left"
p.legend.click_policy="hide"
output_file("interactive_legend.html", title="interactive_legend.py example")
show(p)
source to share
The problem is that it zip
gets truncated at the end of the shorter list.
In [1]: from bokeh.palettes import Spectral11
In [2]: w=['F1','F2','F3','F4','F5','F6','F7','F8','F9','F10','F11','F12','G1','G2','G3','G4','G5','G6','G7','G8'
...: ,'G9','G10','G11','G12']
In [3]: for name, color in zip(w, Spectral11):
...: print(name, color)
...:
F1 #5e4fa2
F2 #3288bd
F3 #66c2a5
F4 #abdda4
F5 #e6f598
F6 #ffffbf
F7 #fee08b
F8 #fdae61
F9 #f46d43
F10 #d53e4f
F11 #9e0142
Selecting a palette longer than your list w
will resolve this (as @PabloReyes mentioned). You can also use itertools.cycle
.
In [4]: import itertools
In [5]: for name, color in zip(w, itertools.cycle(Spectral11)):
...: print(name, color)
...:
F1 #5e4fa2
F2 #3288bd
F3 #66c2a5
F4 #abdda4
F5 #e6f598
F6 #ffffbf
F7 #fee08b
F8 #fdae61
F9 #f46d43
F10 #d53e4f
F11 #9e0142
F12 #5e4fa2
G1 #3288bd
G2 #66c2a5
G3 #abdda4
G4 #e6f598
G5 #ffffbf
G6 #fee08b
G7 #fdae61
G8 #f46d43
G9 #d53e4f
G10 #9e0142
G11 #5e4fa2
G12 #3288bd
You can also use a parameter line_dash
.
In [6]: import bokeh.plotting
...: import numpy as np
...: import pandas as pd
...:
...: bokeh.plotting.output_file('cycle_demo.html')
...: lines = np.random.random((100, len(w))) + np.arange(24)
...: df = pd.DataFrame(lines)
...: df.columns = w
...:
...: line_dash_styles = [[10, 0], [20, 1], [10, 1], [5, 1]]
...: p = bokeh.plotting.figure()
...: for name, color, line_dash in zip(w, itertools.cycle(Spectral11), itertools.cycle(line_dash_styles)):
...: p.line(np.arange(100), df[name], color=color, legend=name, line_dash=line_dash)
...:
...: p.legend.location = "top_left"
...: bokeh.plotting.show(p)
...:
source to share
There are different types of palettes with different numbers of colors. Check: http://bokeh.pydata.org/en/latest/docs/reference/palettes.html . You can choose one of them.
I recommend the ones functions
where you can specify the number of colors you want from a large palette like viridis or inferno.
from bokeh.palettes import inferno
mypalette = inferno(24)
Checking it with random lines in Jupyter notebook:
import bokeh
import bokeh.plotting
import numpy as np
bokeh.io.output_notebook()
lines = np.random.random((24,100))
p = bokeh.plotting.figure()
mypalette24 = bokeh.palettes.inferno(24)
for i,color in enumerate(mypalette24):
p.line(np.arange(100),i+lines[i,:],color=color,legend=str(i+1))
p.legend.location = "top_left"
bokeh.io.show(p)
source to share