Reputation: 2492
So I've basically got this snippet of code:
import pandas as pd
from bokeh.plotting import figure, ColumnDataSource
from bokeh.io import show, output_file
import bokeh.models as bmo
data = {
'ranking': df['ranking'],
'pct_intl_student' : df['pct_intl_student'],
'years': year_list,
'color': colors,
'university':df['university_name']
}
source = ColumnDataSource(data)
hover = bmo.HoverTool(
tooltips=[('year', '@years'),
('ranking', '@ranking'),
('% Int. Stu.', '@pct_intl_student'),
('University', '@university')])
p = figure(tools=[hover], title="Scatterplot: International Students")
p.xaxis.axis_label = 'International Ranking'
p.yaxis.axis_label = 'Pct. International Students'
p.scatter('ranking', 'pct_intl_student', source=source)
show(p)
Where color is basically a list that has a color that corresponds to every datapoint in ranking and pct_intl_student. Basically, they're the same length. Would it be possible to make sure that every datapoint that I plot in the scatterplot has the color as specified in the colors list? I figured it would just be some attribute of the figure, but can't find it easiliy in the documentation. All data is retrieved from a dataframe and I created the color mapping as such:
colormap = {2016: 'red',
2017: 'green',
2018: 'blue'}
colors = [colormap[x] for x in df['year']]
Upvotes: 0
Views: 1560
Reputation: 2492
Okay so I just posed the question but I figured it out in the line:
p.scatter('ranking', 'pct_intl_student', source=source)
You should add: color='color'.
So it looks like this:
p.scatter('ranking', 'pct_intl_student', source=source, color='color')
For sake of completion here's entire code snippet with edits:
colormap = {2016: 'red',
2017: 'green',
2018: 'blue'}
colors = [colormap[x] for x in df['year']]
data = {
'ranking': df['ranking'],
'pct_intl_student' : df['pct_intl_student'],
'years': year_list,
'color': colors,
'university':df['university_name']
}
source = ColumnDataSource(data)
hover = bmo.HoverTool(
tooltips=[('year', '@years'),
('ranking', '@ranking'),
('% Int. Stu.', '@pct_intl_student'),
('University', '@university')])
p = figure(tools=[hover], title="Scatterplot: International Students")
p.xaxis.axis_label = 'International Ranking'
p.yaxis.axis_label = 'Pct. International Students'
p.scatter('ranking', 'pct_intl_student', source=source, color='color')
show(p)
Upvotes: 2