如何使用货币格式化Bokeh Xaxis刻度线 [英] How to format bokeh xaxis ticks with currency
本文介绍了如何使用货币格式化Bokeh Xaxis刻度线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在研究一个juypter笔记本,以制作一个项目符号图来比较两个值.情节渲染,但我想格式化xaxis代码以显示货币.这就是我到目前为止所拥有的...
project =火箭"目标支出= 15000消费2日期= 16600数据= [(项目,目标支出,花费2日期)]从bokeh.io导入显示,output_notebook来自bokeh.plotting导入图def make_bokeh_spend_bullet(数据):output_notebook()如果len(data)== 1:ph = 135elif len(data)> 1和len(data)<3:ph = 165别的:ph =(6 * len(数据)* 10)如果data [0] [2]<数据[0] [1] * 2:性能=数据[0] [2]限制= [0,数据[0] [1],数据[0] [1] * 2]别的:性能=数据[0] [2]限制= [0,数据[0] [1],数据[0] [2]]标签= [确定",超出预算",]cats = [x [0] for data in x]p = figure(title ="Project Spend",plot_height = ph,plot_width = 600,y_range = cats)p.x_range.range_padding = 0p.grid.grid_line_color =无p.xaxis [0] .ticker.num_minor_ticks = 0#p.xaxis [0] .ticker.format('$ 0,0')左,右,zip中的颜色(limits [:-1],limits [1:],OrRd3 [::-1]):p.hbar(y = cats,left = left,right = right,height = 0.8,color = color)对于数据中的x:如果x [2]>x [1]:p.hbar(y = cats,left = 0,right = perf,height = 0.3,color ="firebrick")别的:p.hbar(y = cats,left = 0,right = perf,height = 0.3,color ="gray")comp = [x [1] for data in x]p.segment(x0 = comp,y0 = [(x,-0.5)for x in cats],x1 = comp,y1 = [(对于x,在猫中为[(x,0.5)),color ="black",line_width = 2)首先,用zip标签(limits [:-1],标签):p.add_layout(Label(x = start,y = 0,text = label,text_font_size ="10pt",text_color ='black',y_offset = 5,x_offset = 15))返回节目(p)output_notebook()p = make_bokeh_spend_bullet(数据)
我从
I'm working on a juypter notebook to make a bullet graph to compare two values. The plot renders but I would like to format the xaxis tickers to show currency. This is what I have so far...
project = "Rocket"
targetspend = 15000
spend2date = 16600
data = [(project, targetspend, spend2date)]
from bokeh.io import show, output_notebook
from bokeh.plotting import figure
def make_bokeh_spend_bullet(data):
output_notebook()
if len(data) ==1:
ph = 135
elif len(data) >1 and len(data) < 3:
ph =165
else:
ph = ( 6 * len(data) * 10)
if data[0][2] < data[0][1] * 2:
perf = data[0][2]
limits = [0, data[0][1], data[0][1] *2]
else:
perf = data[0][2]
limits = [0, data[0][1], data[0][2]]
labels = ["OK", "Over budget", ]
cats = [x[0] for x in data]
p=figure(title="Project Spend", plot_height=ph, plot_width=600, y_range=cats)
p.x_range.range_padding = 0
p.grid.grid_line_color = None
p.xaxis[0].ticker.num_minor_ticks = 0
#p.xaxis[0].ticker.format('$0,0')
for left, right, color in zip(limits[:-1], limits[1:], OrRd3[::-1]):
p.hbar(y=cats, left=left, right=right, height=0.8, color=color)
for x in data:
if x[2] > x[1]:
p.hbar(y=cats, left=0, right=perf, height=0.3, color="firebrick")
else:
p.hbar(y=cats, left=0, right=perf, height=0.3, color="gray")
comp = [x[1] for x in data]
p.segment(x0=comp, y0=[(x, -0.5) for x in cats], x1=comp,
y1=[(x, 0.5) for x in cats], color="black", line_width=2)
for start, label in zip(limits[:-1], labels):
p.add_layout(Label(x=start, y=0, text=label, text_font_size="10pt",
text_color='black', y_offset=5, x_offset=15))
return show(p)
output_notebook()
p = make_bokeh_spend_bullet(data)
I read from the bokeh documentation about a class NumeralTickFormatter(**kwargs) but It's not clear how to use this format option for my graph.
解决方案
The code just needs a little more help.
from bokeh.models import Label, Title, NumeralTickFormatter
Then add another assignment to the xaxis
p.xaxis[0].formatter = NumeralTickFormatter(format="$0")
Below is the new graph:
这篇关于如何使用货币格式化Bokeh Xaxis刻度线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文