[英]Using months in x axis in bokeh
假设我有以下数据:
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)
结果没问题。 我想要的是代表一个月的 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")
知道如何超越它吗?
您有两个选择:
您可以使用日期时间轴:
p = figure(plot_width=400, plot_height=400, x_axis_type='datetime')
并将datetime
对象或 unix (seconds-since-epoch) 时间戳值作为 x 值传递。
例如df['month'] = [datetime.date(1900, x, 1) for x in df['month']]
然后 DatetimeTickFormatter 内容将修改标签的格式(完整月份名称、数字月份等)。 这些文档在这里:
第二:
您可以使用分类 xaxis 之类的
p = figure(x_range=['Jan', 'Feb', 'Mar', ...)
对应于您的 x_range 的绘图 x 值,例如:
x = ['Jan', 'Feb', 'Mar', ...]
y = [100, 200, 150, ...]
p.line(x, y)
用户指南在这里涵盖了分类轴:
http://docs.bokeh.org/en/latest/docs/user_guide/plotting.html#categorical-axes
这是一个例子:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.