将 Bokeh 日期时间与 Pandas 结合使用
Using Bokeh datetime with Pandas
是否可以在不使用 pandas 的情况下在散景图中创建 x 轴?
我可以只导入 Python 日期时间模块来创建 xaxis 吗?
在 Bokeh 文档中,他们显示了这一点:
import pandas as pd
from bokeh.plotting import figure, output_file, show
from bokeh.sampledata.stocks import AAPL
df = pd.DataFrame(AAPL)
df['date'] = pd.to_datetime(df['date'])
output_file("datetime.html")
# create a new plot with a datetime axis type
p = figure(plot_width=800, plot_height=250, x_axis_type="datetime")
p.line(df['date'], df['close'], color='navy', alpha=0.5)
show(p)
我正在尝试这样的事情:
from bokeh.io import curdoc
from bokeh.models import ColumnDataSource, DatetimeTickFormatter
from bokeh.plotting import figure
from datetime import datetime
p = figure(plot_height=400, plot_width=1200, x_axis_type="datetime")
p.xaxis.formatter = DatetimeTickFormatter(hours=["%H:%M:%S"],days=["%H:%M:%S"],months=["%H:%M:%S"],years=["%H:%M:%S"])
代码运行但我没有看到任何 x 或 y 轴。
不使用 pandas:
from bokeh.plotting import figure, output_file, show
from bokeh.models import DatetimeTickFormatter
from bokeh.sampledata.stocks import AAPL
import datetime
output_file("datetime.html")
AAPL['date'] = [datetime.datetime.strptime(x, "%Y-%m-%d").date() for x in AAPL['date']]
p = figure(plot_width=800, plot_height=250)
p.xaxis[0].formatter = DatetimeTickFormatter()
p.line(AAPL['date'], AAPL['close'], color='navy', alpha=0.5)
show(p)
是否可以在不使用 pandas 的情况下在散景图中创建 x 轴? 我可以只导入 Python 日期时间模块来创建 xaxis 吗?
在 Bokeh 文档中,他们显示了这一点:
import pandas as pd
from bokeh.plotting import figure, output_file, show
from bokeh.sampledata.stocks import AAPL
df = pd.DataFrame(AAPL)
df['date'] = pd.to_datetime(df['date'])
output_file("datetime.html")
# create a new plot with a datetime axis type
p = figure(plot_width=800, plot_height=250, x_axis_type="datetime")
p.line(df['date'], df['close'], color='navy', alpha=0.5)
show(p)
我正在尝试这样的事情:
from bokeh.io import curdoc
from bokeh.models import ColumnDataSource, DatetimeTickFormatter
from bokeh.plotting import figure
from datetime import datetime
p = figure(plot_height=400, plot_width=1200, x_axis_type="datetime")
p.xaxis.formatter = DatetimeTickFormatter(hours=["%H:%M:%S"],days=["%H:%M:%S"],months=["%H:%M:%S"],years=["%H:%M:%S"])
代码运行但我没有看到任何 x 或 y 轴。
不使用 pandas:
from bokeh.plotting import figure, output_file, show
from bokeh.models import DatetimeTickFormatter
from bokeh.sampledata.stocks import AAPL
import datetime
output_file("datetime.html")
AAPL['date'] = [datetime.datetime.strptime(x, "%Y-%m-%d").date() for x in AAPL['date']]
p = figure(plot_width=800, plot_height=250)
p.xaxis[0].formatter = DatetimeTickFormatter()
p.line(AAPL['date'], AAPL['close'], color='navy', alpha=0.5)
show(p)