我有一个 Excel 工作表,我使用 pandas 库从中提取“TIME”和“TEMP”列。我想绘制温度与时间图,我使用了 Bokeh 图。然而, Bokeh 图并没有完全显示 X 轴值。相反,它只显示“TIME”列的最后几个字符,即 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”
Bokeh 图 -
如您所见, Bokeh 图 x 轴值未完全绘制。请帮忙。
最佳答案
该图似乎正确显示了您的数据,但您应该检查以确保 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
关于python - 如何设置 x 标签以在 Bokeh 图中显示日期时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47119285/