我有一个半复杂的散景服务器图,下面的代码是简化的 . 我通过matplotlib颜色的随机选择为散景字形指定颜色 . 我想指定一个按钮,为点击时的字形生成一组新的颜色 .
示例数据帧
FULLNAME | TX | EX | YEAR
Ted 12 33 2000
Fred 20 7 2000
...
Jeb 8 8 2001
import matplotlib
import random
from datetime import date
import pandas as pd
from bokeh.models import (Legend, CategoricalColorMapper, CDSView,NumeralTickFormatter,
GroupFilter,Slider,Select, DatetimeTickFormatter, Button, OpenURL, TapTool)
from bokeh.plotting import figure
from bokeh.io import curdoc
user_list= list(set((df["FULLNAME"])))
#initial colors
colordict = dict(matplotlib.colors.cnames.items())
colordict = list(colordict.values())
random_colors = random.sample(colordict, len(user_list))
def yr_slider():
current=slider.value
p = figure(plot_width=930, plot_height=325)
p.circle(x=df["YEAR"][df["YEAR"]==current], y=df["EX"][df["YEAR"]==current],color=random_colors)
return p
def update(attr, old, new):
layout2.children[1]=yr_slider()
slider=Slider(start=2017, end=date.today().year, step=1, value=date.today().year)
slider.on_change('value', update)
def updateColrs(): #DEFINE NEW RANDOM COLORS for PLOT, NOT WORKING
colordict = dict(matplotlib.colors.cnames.items())
colordict = list(colordict.values())
random_colors = random.sample(colordict, len(user_list))
new_plot = yr_slider(random_colors)
return random_colors
button=Button(label="Change Colors", button_type='success')
button.on_click(updateColrs)
ctrls=widgetbox([slider, button], width=200)
layout2=row(ctrls,yr_slider())
curdoc().add_root(layout2)
函数 UpdateColrs 并不像它应该的那样工作 . 我已经尝试将 random_colors=UpdateColrs 放在 yr_slider(): 中,但是,这会更改滑块交互的颜色,这是不需要的 . 如何才能在按钮单击上严格绘制新的随机颜色?
提前致谢 .