Skip to main content
 首页 » 编程设计

python之Bokeh 图标签增量

2024年10月24日11findumars

我正在尝试使用在 Bokeh 教程中找到的一些代码来消除图表 X 轴上的无关标签。显然我遗漏了一些东西,因为我收到以下错误:

AttributeError: 'Chart' 对象没有属性 'xaxis'

我假设问题是高级图表不像常规图表那样起作用。有没有解决的办法?

import numpy 
 
 
''' 
***************** Craps Array Functions***************** 
''' 
 
def rollDie(n): 
    import random     
    die = random.randint(1,n) 
    return die 
 
def crapsRoll():  
    a = rollDie(6) 
    b = rollDie(6) 
    c = a + b 
    return(c) 
 
def crapsArray(count) :    
    import numpy as np 
    rollcount = 0 
    inc = 0 
    rolls = [] 
    row = [] 
    while inc < count: 
        inc = inc + 1 
        rollcount = rollcount + 1 
        roll = crapsRoll() 
        row = [rollcount, roll] 
        rolls.append(row)      
    else: 
        rollsArray = np.asarray(rolls) 
        print(' Rolls Done')     
 
    return(rollsArray) 
 
temp = crapsArray(100)        
numpy.savetxt('test.csv', temp, fmt= '%3.0d', delimiter = ',', header = "roll,score", comments ='') 
 
''' 
********************* Bokeh Plots *********************** 
''' 
import pandas as pd 
from bokeh.charts import Bar, output_file, show 
from bokeh.plotting import figure  
from bokeh.models import FixedTicker 
 
csv=pd.read_csv('test.csv') 
 
output_file("craps.html") 
 
p = figure(plot_width = 1000, plot_height = 400) 
 
p = Bar(csv, 'roll', values = 'score', title = "Craps Outcomes", bar_width = 1, color = 'roll') 
p.xaxis[0].ticker=FixedTicker(ticks=[0,25,50,75,100]) 
 
show(p) 

请您参考如下方法:

xaxis等方便的方法在bokeh.plotting.Figure上。然而图表是基础 bokeh.models.Plot 基类的子类,因此它们不存在。不过,您可以使用 select 方法来查询对象:

In [18]: from bokeh.models import LinearAxis 
 
In [19]: p.select(LinearAxis) 
Out[19]: [<bokeh.models.axes.LinearAxis at 0x108f4def0>] 

让图表上的轴变得更容易可能是合理的,请随时在 GitHub 上打开一个功能请求问题,以便其他核心开发人员可以看到和讨论它。