有没有办法让散景自动调整哪些刻度标签可见,因为缩放级别变化以保持恒定数量的刻度标签可见?
vIEws.py:
# -*- Coding: utf-8 -*-from django.shortcuts import renderfrom bokeh.plotting import *from bokeh.resources import CDNfrom bokeh.embed import componentsfrom bokeh.charts import barfrom bokeh.models import Range1dfrom collections import OrderedDictimport datetimedef plot_1_bar(data,y,y_lab,Title,xlab,ylab,yrange,palette): x0_List_str = [] y_List = [] # x_axis_type == "datetime": i_x = 'x1' i_y = y[0] for IDx,val in enumerate(data): x0_List_str.append(val[i_x].strftime("%Y-%m-%d %H h")) y_List.append(val[i_y]) data_x = x0_List_str data_y = OrderedDict() data_y[y_lab[0]] = y_List plot = bar(data_y,data_x,Title=Title,xlabel=xlab,ylabel=ylab,stacked=True,continuous_range=yrange,palette=palette,legend=True,height=400) return components(plot,CDN)def plot1(request): script_bokeh = "" div_bokeh = "" data = [] data.append({'x1': datetime.datetime(2015,1,8,0),'y1': 0}) data.append({'x1': datetime.datetime(2015,9,10,11,12,'y1':8}) data.append({'x1': datetime.datetime(2015,13,'y1':12}) data.append({'x1': datetime.datetime(2015,14,'y1':0}) data.append({'x1': datetime.datetime(2015,15,16,17,18,'y1':2}) data.append({'x1': datetime.datetime(2015,19,'y1':6}) data.append({'x1': datetime.datetime(2015,20,'y1':4}) data.append({'x1': datetime.datetime(2015,21,'y1':22}) data.append({'x1': datetime.datetime(2015,22,'y1':36}) data.append({'x1': datetime.datetime(2015,23,'y1':32}) data.append({'x1': datetime.datetime(2015,2,'y1':21}) data.append({'x1': datetime.datetime(2015,'y1':15}) data.append({'x1': datetime.datetime(2015,3,4,5,6,'y1':45}) data.append({'x1': datetime.datetime(2015,7,'y1':47}) data.append({'x1': datetime.datetime(2015,'y1':52}) data.append({'x1': datetime.datetime(2015,'y1':44}) data.append({'x1': datetime.datetime(2015,'y1':0}) script_bokeh,div_bokeh = plot_1_bar(data,['y1'],['y legend label'],"",'x label','y label',Range1d(0,70),['red']) return render(request,'tests/plot1.HTML',{'script_bokeh': script_bokeh,'div_bokeh': div_bokeh})
plot1.HTML:
<!DOCTYPE HTML><HTML lang="en"> <head> <Title>bokeh bar plot</Title> <link rel="stylesheet" type="text/CSS" @R_403_6839@="/static/tests/CSS/bokeh-0.9.2.min.CSS" /> <script src="/static/tests/Js/bokeh-0.9.2.min.Js" type="text/JavaScript"></script> </head> <body> <h1>bokeh bar plot</h1> <div ID="div_graph">{{div_bokeh|safe}}{{script_bokeh|safe}}</div> </body></HTML>解决方法 首先,bokeh.charts API(包括bar)在2017年已弃用并删除,不应使用.稳定且受支持的bokeh.plotting API现在可用于创建多种分类和条形图,并且比bokeh.charts更强大.有关完整代码的许多实例,请参见用户指南第 Handling Categorical Data章
没有内置的自动收报机可以配置为执行此 *** 作,并且没有任何CustomJsTicker(尚未)所以,现在要做这样的事情你将不得不求助于custom extension.此外,你将需要CustomJs回调范围,该集合根据您所需的策略配置自定义代码.这是一个完整的脚本,显示了一种可能的方法(此代码使用bokeh 0.12.16进行测试):
from bokeh.core.propertIEs import Intfrom bokeh.models import CategoricalTicker,CustomJsfrom bokeh.io import showfrom bokeh.plotting import figureclass MyTicker(CategoricalTicker): __implementation__ = """ import {CategoricalTicker} from "models/tickers/categorical_ticker" import * as p from "core/propertIEs" export class MyTicker extends CategoricalTicker type: "MyTicker" @define { nth: [ p.Int,1 ] } get_ticks: (start,end,range,cross_loc) -> ticks = super(start,cross_loc) ticks.major = ticks.major.filter((element,index) => index % this.nth == 0) return ticks """ nth = Int(default=1)fruits = ['Apples','Pears','Nectarines','Plums','Grapes','StrawberrIEs']p = figure(x_range=fruits,plot_height=250,title="Fruit Counts",tools="xwheel_zoom")p.vbar(x=fruits,top=[5,6],wIDth=0.9)p.xgrID.grID_line_color = Nonep.y_range.start = 0p.xaxis.ticker = MyTicker(nth=1)cb = CustomJs(args=dict(ticker=p.xaxis[0].ticker),code=""" if (Math.abs(cb_obj.start-cb_obj.end) > 8) { ticker.nth = 2 } else { ticker.nth = 1 }""")p.x_range.Js_on_change('start',cb)p.x_range.Js_on_change('end',cb)show(p)总结
以上是内存溢出为你收集整理的python – 当散景条图上的缩放级别变化时,是否可以保持相同数量的x轴刻度标签可见?全部内容,希望文章能够帮你解决python – 当散景条图上的缩放级别变化时,是否可以保持相同数量的x轴刻度标签可见?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)