在bokeh中的x轴上使用月份 [英] Using months in x axis in bokeh

查看:65
本文介绍了在bokeh中的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)

我正在使用bokeh可视化结果:

I am using bokeh to visualize the results:

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

结果还可以.我想要的是x轴来表示一个月(1:January,2:February ..).我正在执行以下操作以将数字转换为月数:

The results are ok. What I want is the x axis to represent a month(1:January,2:February..). I am doing the following to convert the numbers to months:

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)

结果为空.以下内容也不起作用:

The results is an empty figure. The following is also not working:

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

有什么想法可以超越它吗?<​​/p>

Any idea how to overpass it?

推荐答案

您有两个选择:

您可以使用日期时间轴:

You can use a datetime axis:

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

并传递datetime对象或unix(秒后)的时间戳值作为x值.

And pass either datetime objects or unix (seconds-since-epoch) timestamps values as x-values.

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

然后,DatetimeTickFormatter东西将修改标签的格式(完整的月份名称,数字月份等).这些文档在这里:

The DatetimeTickFormatter stuff will then modify the formatting of labels (full month name, numeric month, etc). Those docs are here:

http://docs.bokeh.org/en/latest/docs/reference/models/formatters.html#bokeh.models.formatters.DatetimeTickFormatter

第二:

您可以使用

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

与您的x_range相对应的绘图x值,例如:

The plot x-values that correspond to your x_range, like:

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

用户指南在此处介绍了分类轴:

The user guide covers categorical axes here:

http://docs.bokeh.org/en/latest/docs/user_guide/plotting.html#categorical-axes

这是一个例子:

这篇关于在bokeh中的x轴上使用月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆