【问题标题】:How can I set the x-label to display datetime in a Bokeh plot?如何设置 x-label 在散景图中显示日期时间?
【发布时间】:2018-04-17 13:46:01
【问题描述】:

我有一个 Excel 表,我使用 pandas 库从中提取“TIME”和“TEMP”列。我想绘制使用 Bokeh plot 的温度与时间图。但是,散景图并未完全显示 X 轴值。相反,它只显示“时间”列的最后几个字符,即 x 轴。请帮忙 -

Python 代码 -

import pandas as pd
from bokeh.plotting import figure,output_file,show
from bokeh.models.ranges import Range1d

results=pd.read_excel("test.xls",parse_dates=["TIME"])
print(results['TIME'])

p=figure(plot_width=900,plot_height=500,x_axis_type="datetime",x_axis_label="TIME",y_axis_label="TEMPERATURE" )
p.vbar(x=results["TIME"],top=results["TEMP"],color="red",width=2.4,bottom=0)

p.y_range=Range1d(0,150)
output_file("Scatter_plotting.html")
show(p)

Excel 文件 - “test.xls”

散景图 -

如您所见,散景图 x 轴值未完全绘制。请帮忙。

【问题讨论】:

    标签: python pandas plot bokeh


    【解决方案1】:

    该图似乎正确显示了您的数据,但您应该检查以确保 results['TIME'] 是 Pandas 日期时间格式(您的打印语句应在底部显示 dtype: datetime64[ns])。

    如果格式正确,则可以通过以下方式控制 x-ticks:

    from bokeh.models import DatetimeTickFormatter
    
    p.xaxis.formatter = DatetimeTickFormatter(hourmin = ['%H:%M']) # Or whatever format you want to use...
    

    在此处查看文档:

    https://docs.bokeh.org/en/latest/docs/reference/models/formatters.html

    【讨论】:

    • 起初,hourmin 关键字似乎应该指定您希望如何显示所有数据。我花了一点时间才意识到它实际上是在定义你想要的 hourmin 缩放级别。因此,如果您希望它使用与人们放大/缩小相同的格式,请将适用于您的数据的不同缩放级别设置为相同的格式,如this related post
    • 2021 年(Holoviews v1.14.5)正确的格式是:curve.opts(xformatter=DatetimeTickFormatter(hourmin = ['%H:%M'])) per holoviews.org/user_guide/Customizing_Plots.html#Tick-formatters
    猜你喜欢
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    相关资源
    最近更新 更多