关闭散景中的刻度标记

时间:2014-11-26 04:17:20

标签: python bokeh

我正在研究Bokeh(0.6.1)教程并尝试关闭其中一个练习图中的刻度线和标签,the scatter plot

from __future__ import division

import numpy as np
from six.moves import zip
from bokeh.plotting import *
from bokeh.objects import Range1d

output_file("scatter.html")

figure()

N = 4000

x = np.random.random(size=N) * 100
y = np.random.random(size=N) * 100
radii = np.random.random(size=N) * 1.5
colors = [
    "#%02x%02x%02x" % (r, g, 150) 
    for r, g in zip(np.floor(50+2*x), np.floor(30+2*y))
]

circle(x, y, radius=radii,
       fill_color=colors, fill_alpha=0.6,
       line_color=None, Title="Colorful Scatter")

grid().grid_line_color = None
axis().axis_line_color = None

# QUESTION PART 1: Is this the right way to turn off tick labels?
axis().major_label_text_font_size = '0pt'  
# QUESTION PART 2: ...and how to turn off tick marks also?

show()  # open a browser

我已设法关闭刻度标签但没有搜索文档和谷歌搜索的数量已显示关闭刻度线所需的咒语。

此外,我不确定将axis().major_label_text_font_size设置为0pt是关闭刻度标签的正确方法,还是将其设置为kludge。似乎没有其他工作。

我错过了一些明显的东西吗?

2 个答案:

答案 0 :(得分:12)

这个答案是最新的0.124版Bokeh的更新。有关其他信息,请从Styling Visual AttributesBokeh documentation页面获取这些命令。

要关闭主要和次要刻度线,请将其颜色设置为None

p = bokeh.plotting.figure(plot_width=400, plot_height=400)
p.circle([1,2,3,4,5], [2,5,8,2,7], size=10)

p.xaxis.major_tick_line_color = None  # turn off x-axis major ticks
p.xaxis.minor_tick_line_color = None  # turn off x-axis minor ticks

p.yaxis.major_tick_line_color = None  # turn off y-axis major ticks
p.yaxis.minor_tick_line_color = None  # turn off y-axis minor ticks

要关闭刻度标签,请将字体大小设置为'0pt'

p.xaxis.major_label_text_font_size = '0pt'  # turn off x-axis tick labels
p.yaxis.major_label_text_font_size = '0pt'  # turn off y-axis tick labels

通过将字体颜色设置为“无”来实现类似的结果,但缺点是仍然为刻度标签维护空间。

p.xaxis.major_label_text_color = None  # turn off x-axis tick labels leaving space
p.yaxis.major_label_text_color = None  # turn off y-axis tick labels leaving space 

此代码段举例说明了删除主要和次要刻度线以及刻度标签。

import bokeh.io
import bokeh.plotting
import bokeh.layouts
bokeh.io.output_file('remove_tick_marks.html')

p0 = bokeh.plotting.figure(plot_width=200, plot_height=200, 
                           x_axis_label='x', y_axis_label='y', 
                           title='original')
p0.circle([1,2,3,4,5], [2,5,8,2,7], size=10)

p1 = bokeh.plotting.figure(plot_width=200, plot_height=200, 
                           x_axis_label='x', y_axis_label='y', 
                           title='remove tick marks')
p1.circle([1,2,3,4,5], [2,5,8,2,7], size=10)
p1.xaxis.major_tick_line_color = None  # turn off x-axis major ticks
p1.xaxis.minor_tick_line_color = None  # turn off x-axis minor ticks
p1.yaxis.major_tick_line_color = None  # turn off y-axis major ticks
p1.yaxis.minor_tick_line_color = None  # turn off y-axis minor ticks

p2 = bokeh.plotting.figure(plot_width=200, plot_height=200, 
                           x_axis_label='x', y_axis_label='y', 
                           title='remove tick labels')
p2.circle([1,2,3,4,5], [2,5,8,2,7], size=10)
p2.xaxis.major_tick_line_color = None  # turn off x-axis major ticks
p2.xaxis.minor_tick_line_color = None  # turn off x-axis minor ticks
p2.yaxis.major_tick_line_color = None  # turn off y-axis major ticks
p2.yaxis.minor_tick_line_color = None  # turn off y-axis minor ticks
p2.xaxis.major_label_text_font_size = '0pt'  # preferred method for removing tick labels
p2.yaxis.major_label_text_font_size = '0pt'  # preferred method for removing tick labels

p3 = bokeh.plotting.figure(plot_width=200, plot_height=200, 
                           x_axis_label='x', y_axis_label='y', 
                           title='notice extra space')
p3.circle([1,2,3,4,5], [2,5,8,2,7], size=10)
p3.xaxis.major_tick_line_color = None  # turn off x-axis major ticks
p3.xaxis.minor_tick_line_color = None  # turn off x-axis minor ticks
p3.yaxis.major_tick_line_color = None  # turn off y-axis major ticks
p3.yaxis.minor_tick_line_color = None  # turn off y-axis minor ticks
p3.xaxis.major_label_text_color = None  #note that this leaves space between the axis and the axis label  
p3.yaxis.major_label_text_color = None  #note that this leaves space between the axis and the axis label  

grid = bokeh.layouts.gridplot([[p0, p1, p2, p3]])
bokeh.io.show(grid)

enter image description here

答案 1 :(得分:6)

我不确定一个多星期以来缺少答案是由于人们不知道答案,还是因为这个问题被忽视太明显了。

无论如何,希望其他人发现它有用,我发布这个答案。我找到了一种方法,这看起来就像一个黑客,我只发布它,希望有人会改进它...

from __future__ import division

import numpy as np
from six.moves import zip
from bokeh.plotting import *

output_file("scatter.html")

figure()

N = 4000

x = np.random.random(size=N) * 100
y = np.random.random(size=N) * 100
radii = np.random.random(size=N) * 1.5
colors = ["#%02x%02x%02x" % (r, g, 150) 
          for r, g in zip(np.floor(50+2*x), np.floor(30+2*y))]

circle(x, y, radius=radii,
       fill_color=colors, fill_alpha=0.6,
       line_color=None, Title="Colorful Scatter")

grid().grid_line_color = None
axis().axis_line_color = None
curplot().outline_line_color = None

# Turn off tick labels
axis().major_label_text_font_size = '0pt'  
# Turn off tick marks 
axis().major_tick_line_color = None  # turn off major ticks
axis()[0].ticker.num_minor_ticks = 0  # turn off minor ticks
axis()[1].ticker.num_minor_ticks = 0

show()  # open a browser