我创建了一个折线图,其x轴为年月格式(“ 201801”),为列表格式。图表显示良好。当我尝试添加日期范围滑块时,出现了一个错误,那就是月份应该在1到12之间。
我尝试将年份月份转换为DateTime,但是仍然无法以所需的功能显示滑块。
monthly_data= data[[var_YEARMONTH,var_TARGET, var_SEGMENT, var_MARKET]]
#monthly_data=monthly_data[(monthly_data[var_YEARMONTH]>=201801) &
(monthly_data[var_YEARMONTH]<=201812)]
source = ColumnDataSource(monthly_data)
yearmonth = source.data[var_YEARMONTH].tolist()
# ym=pd.unique(yearmonth)
# print(pd.Series(ym).value_counts())
for i in range(0,len(yearmonth)):
yearmonth[i]=(str(yearmonth[i]))
# print("%%%%%%%%%%%%%",yearmonth[i])
# yearmonth[i] = datetime.strptime(yearmonth[i], '%Y%m')
# print("%%%%%%%%%%%%%",yearmonth[i])
yearmonth=pd.unique(yearmonth)
yearmonth=yearmonth.tolist()
yearmonth.sort()
print(type(yearmonth))
#yearmonth=date_text(yearmonth)
#source = ColumnDataSource(monthly_data)
#segment = source.data[var_SEGMENT].tolist()
#for i in range(0,len(segment)):
# segment[i]=str(segment[i])
#segment=pd.unique(segment)
#segment=data[var_SEGMENT].unique().tolist()
segment=['Brand TRx','Market TRx']
trx_by_months=monthly_data.groupby([var_YEARMONTH],as_index=False).agg({var_TARGET: np.sum, var_MARKET:np.sum})
trx_by_months['Brand Share']=(trx_by_months[var_TARGET]/(trx_by_months[var_TARGET]+trx_by_months[var_MARKET]))*100
trx_by_months['Market TRx']=trx_by_months[var_MARKET]+trx_by_months[var_TARGET]
trx_by_months['Brand TRx']=trx_by_months[var_TARGET]
# seg_df=trx_by_months[[var_YEARMONTH,'Brand TRx','Market TRx']]
# seg_df[var_YEARMONTH]=seg_df[var_YEARMONTH].astype(str)
# seg_df[var_YEARMONTH]=date_text(seg_df[var_YEARMONTH])
# # for i in range(0,len(seg_df['Brand TRx'])):
# # seg_df['Brand TRx'][i]=millify(seg_df['Brand TRx'][i])
# # for i in range(0,len(seg_df['Market TRx'])):
# # seg_df['Market TRx'][i]=millify(seg_df['Market TRx'][i])
# seg_data=seg_df.to_dict('list')
# startdate=[]
# enddate=[]
# startdate.append(yearmonth[0])
# enddate.append(yearmonth[-1])
# print(startdate);print(type(startdate))
# startdate=yearmonth[0]
# enddate=yearmonth[-1]
def to_datetime(yearmonth):
year = int(yearmonth[0:4])
month = int(yearmonth[5:7])
day = int("01")
return datetime(year, month, day)
# filter by countries with at least one medal and sort
#trx=trx_by_months[var_TARGET]
p = figure(plot_height=250,plot_width=1000, title="TRx by Month and Brand Share",
toolbar_location=None, tools="")
# p.vbar_stack(segment, x=var_YEARMONTH, width=0.9,color=colors, source=seg_data,
# legend=[value(x) for x in segment])
# colors= chart_colors[:len(segment)]
# r1=p.vbar_stack(segment, x=var_YEARMONTH, width=0.9,color=colors, source=seg_data)#,
# # legend=[value(x) for x in segment])
#trx_by_months[var_YEARMONTH] = pd.to_datetime(trx_by_months[var_YEARMONTH])
#arr=[2018-01-01 00:00:00, 2018-02-01 00:00:00]
p.line(yearmonth, trx_by_months['Brand Share'], line_width=2, line_color='red', legend='% Brand Share')
p.xgrid.grid_line_color = None
p.y_range.start = 0
#p.xaxis.axis_label = 'Year Month'
p.yaxis.axis_label = '% Brand Share'
# p.extra_y_ranges = {"avg": Range1d(start=0, end=14)}
# p.add_layout(LinearAxis(y_range_name="avg", axis_label='% Brand Share'), 'right')
#p.legend.location = "bottom_right"
# legend = Legend(items=[
# (YM, [r]) for (YM, r) in zip(segment, r1)],
# #('% Brand Share',[r2])],
# location=(0, 30))
# legend1 = Legend(items=[
# #(YM, [r]) for (YM, r) in zip(segment, r1)],
# ('% Brand Share',[r1])],
# location=(-135, -10))
# #p.add_layout(legend, 'right')
# p.add_layout(legend1, 'right')
#p.left[0].formatter.use_scientific = False
#Set up widgets
#text = TextInput(title="title", value='trx by month')
#offset = Slider(title="offset", value=0.0, start=-5.0, end=5.0,step=0.1)
#amplitude = Slider(title="amplitude", value=1.0, start=-5.0, end=5.0)
#phase = Slider(title="phase", value=0.0, start=0.0, end=2*np.pi)
#freq = Slider(title="frequency", value=1.0, start=0.1, end=5.1)
#box = BoxAnnotation(fill_alpha=0.5, line_alpha=0.5, level='underlay', left=startdate, right=enddate)
def update_range(attr, old, new):
#date_range_slider.left = new[0]
#date_range_slider.right = new[1]
p.x_range.start = new[0]
p.x_range.end = new[1]
date_range_slider = DateRangeSlider(title="Date Range: ", start=min(yearmonth), end=max(yearmonth), value=(min(yearmonth), max(yearmonth)), width=500)
date_range_slider.on_change('value',update_range)
#l = layout(children=[[date_range_slider]], sizing_mode='fixed')
#curdoc().title = "DateRangeSlider Example"
# Set up callbacks
# def update_title(attrname, old, new):
# plot.title.text = text.value
# text.on_change('value', update_title)
# def update_data(attrname, old, new):
# # Get the current slider values
# # a = amplitude.value
# # b = offset.value
# w = phase.value
# k = freq.value
# Generate the new curve
# for w in [phase, freq]:
# w.on_change('value', update_data)
# Set up layouts and add to document
inputs = widgetbox(date_range_slider)
#p.add_layout(box)
curdoc().add_root(layout(inputs,p))