2016-02-24 85 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