2016-02-24 86 views
2

x轴使用几个月可以说我有以下数据:在背景虚化

import random 
import pandas as pd 
numbers = random.sample(range(1,50), 12) 
d = {'month': range(1,13),'values':numbers} 
df = pd.DataFrame(d) 

我使用背景虚化以可视化的结果:

p = figure(plot_width=400, plot_height=400) 
p.line(df['month'], df['values'], line_width=2) 
output_file('test.html') 
show(p) 

enter image description here

的结果是OK 。我想要的是x轴代表一个月(1:1,2:2 ..)。我做了以下的数字转换成月:

import datetime 
df['month'] = [datetime.date(1900, x, 1).strftime('%B') for x in df['month']] 
p = figure(plot_width=400, plot_height=400) 
p.line(df['month'], df['values'], line_width=2) 
show(p) 

结果是空的身影。以下是还没有工作:

p.xaxis.formatter = DatetimeTickFormatter(format="%B") 

任何想法如何立交桥呢?

回答

3

有两个选项:

可以使用一个日期时间轴:

p = figure(plot_width=400, plot_height=400, x_axis_type='datetime') 

又通任datetime对象或UNIX(秒-因为历元)时间戳值作为x值。

例如df['month'] = [datetime.date(1900, x, 1) for x in df['month']]

DatetimeTickFormatter的东西然后将修改标签的格式(全月名称,数字月份等)。这些文档是在这里:http://bokeh.pydata.org/en/latest/docs/reference/models/formatters.html#bokeh.models.formatters.DatetimeTickFormatter

二:

你可以种用一个明确的x轴状

p = figure(x_range=['Jan', 'Feb', 'Mar', ...) 

情节的x值对应于您的x_range,如:

x = ['Jan', 'Feb', 'Mar', ...] 
y = [100, 200, 150, ...] 
p.line(x, y) 

用户指南涵盖分类轴:http://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html#categorical-axes

下面是一个例子:

Categorical Axis example