Python 在bokeh的x轴中使用月份
假设我有以下数据:Python 在bokeh的x轴中使用月份,python,bokeh,Python,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) 我使用bokeh将结果可视化: p = figure(plot_width=400, plot_height=400) p.line(df['month'], df['values'], lin
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将结果可视化:
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:一月,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(自纪元起的秒数)时间戳值作为x值传递
e、 g.df['month']=[datetime.date(1900,x,1)表示df['month']中的x。]
DatetimeTickFormatter将修改标签的格式(完整月份名称、数字月份等)。这些文件在这里:
第二:
你可以用一种分类的xaxis,比如
p = figure(x_range=['Jan', 'Feb', 'Mar', ...)
对应于x_范围的绘图x值,如:
x = ['Jan', 'Feb', 'Mar', ...]
y = [100, 200, 150, ...]
p.line(x, y)
《用户指南》在此处介绍了分类轴:
下面是一个例子: