Reputation: 41
I recently started using Bokeh for interactive network visualization. I'm plotting coordinates for 50 points, nodes that represent machines. Below is image of how my data is represented and my code. (I only put 14 machines to be simpler).
I've managed to plot the points but I have a question that I didn't find anywhere a specific solution. For some machines I have its temperature, but others no. How can I make the machines that I have the temperature information have a different color?
Like, the ones that I have this information be red, and the others that I don't have the information be blue? All tutorials that I found about changing the nodes color involves palettes, but this wouldn't have much use for me now.
import pandas
from bokeh.io import output_notebook, show, save
from bokeh.io import output_notebook, show, save
from bokeh.models import Range1d, Circle, ColumnDataSource, MultiLine
from bokeh.plotting import figure
output_notebook()
from bokeh.plotting import ColumnDataSource, figure, output_file, show
df = pandas.read_excel('Pasta1.xlsx', engine='openpyxl')
source = ColumnDataSource(data=dict(x = df['x'] ,y = df['y']))
TOOLTIPS = [("index", "$index"),("(x,y)", "($x, $y)")]
p = figure(width=1000, height=500, tooltips=TOOLTIPS,title="Redes")
p.circle('x', 'y', size=10,fill_color='red', source=source)
show(p)
Upvotes: 0
Views: 1003
Reputation: 6337
The solutions is to use the color
keyword of the p.circle()
and pass a list (or array) instead of a static value. If you now the rules for your color it should be easy to pass the information to your plot.
The keyword fill_color
also accepts lists (or arrays), if you prefere this.
Example
The example below creates the color
column in the pandas DataFrame first, using np.where()
. This can be done also by hand or using other technics.
import numpy as np
import pandas as pd
from bokeh.io import output_notebook, show, save
from bokeh.models import Range1d, Circle, ColumnDataSource, MultiLine
from bokeh.plotting import figure
output_notebook()
df = pd.DataFrame({
'machine':['J'+str(i) for i in range(13)],
'x':list(range(13)),
'y':list(range(13)),
'Temp' : [np.nan, 32, np.nan, 33, np.nan, np.nan, np.nan, np.nan, 35, np.nan, np.nan, 32, np.nan]
})
df['color'] = np.where(df['Temp'].isna(), 'blue', 'red')
source = ColumnDataSource(df)
TOOLTIPS = [("index", "$index"),("(x,y)", "($x, $y)"), ('name', "@machine")]
p = figure(width=500, height=500, tooltips=TOOLTIPS,title="Redes")
p.circle(x='x', y='y', size=10, color='color', source=source)
show(p)
Output
Upvotes: 1