Python 颜色基于波基的年份
我基本上得到了这段代码:Python 颜色基于波基的年份,python,pandas,bokeh,Python,Pandas,Bokeh,我基本上得到了这段代码: 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'],
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)
其中颜色基本上是一个列表,其颜色对应于排名和pct_intl_student中的每个数据点。基本上,它们的长度是一样的。是否可以确保我在散点图中绘制的每个数据点都具有颜色列表中指定的颜色?我想这只是图的一些属性,但在文档中找不到它。所有数据都从数据框中检索,我创建了如下颜色映射:
colormap = {2016: 'red',
2017: 'green',
2018: 'blue'}
colors = [colormap[x] for x in df['year']]
好吧,我只是提出了这个问题,但我在台词中找到了答案:
p.scatter('ranking', 'pct_intl_student', source=source)
您应该添加:color='color'
看起来是这样的:
p.scatter('ranking', 'pct_intl_student', source=source, color='color')
为了便于完成,下面是经过编辑的完整代码段:
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)