gpt4 book ai didi

python - 在 Bokeh 中,奇怪的日期轴问题

转载 作者:太空宇宙 更新时间:2023-11-04 05:36:43 24 4
gpt4 key购买 nike

我正在尝试通过 pandas 使用 Bokeh 绘制一些数据。 x 轴是日期,我可以让 Bokeh 绘制“大部分”正确的轴(范围可能会关闭)。然而,它输出的行到处都是。

例如:

bokeh plot line issue

看起来可能是一条连续的大线?

这是我的代码:

# library imports

import pandas as pd
from bokeh.io import output_file, show, vform
from bokeh.plotting import figure, output_file, ColumnDataSource, show
from bokeh.models import HoverTool, BoxAnnotation, BoxSelectTool, BoxZoomTool, WheelZoomTool, ResetTool

# Import csv into pandas dataframe

df = pd.read_csv(r"C:\Users\paul.shapiro\Documents\kwdata.csv", parse_dates=['Interest over time_time'])
df.rename(columns={'Search Term': 'keyword', 'Interest over time_time': 'date', 'Weekly Volume': 'volume'}, inplace=True)

source = ColumnDataSource(data=dict(x=df['date'], y=df['volume'], desc=df['keyword']))
TOOLS = [HoverTool(tooltips=[("Keyword", "@desc"),("Date", "@x"),("Search Volume", "@y")]), BoxZoomTool(), WheelZoomTool(), ResetTool()]

# Output html for embedding
output_file("line.html")

p = figure(plot_width=800, plot_height=800, tools=TOOLS, x_axis_type="datetime")

# add both a line and circles on the same plot
p.line(df['date'], df['volume'], line_width=2, color=df['keyword'], source=source)
p.circle(df['date'], df['volume'], fill_color="white", size=8, source=source)

show(p)

同样值得注意的是,如果您使用 bokeh.charts 绘制它(如果我这样做,工具提示将不起作用,所以它不是一个选项),它绘制得很好:

using bokeh.charts instead

defaults.width = 800
defaults.height = 800

TOOLS = [BoxZoomTool(), WheelZoomTool(), ResetTool()]

line = Line(df, x='date', y='volume', color='keyword', source=source, tools=TOOLS)
show(line)
output_file("line.html", title="Search Volume")

任何帮助将不胜感激。这让我发疯了!


已解决 使用 multi_line() 和 for 循环:

import pandas as pd
from bokeh.io import output_file, show, vform
from bokeh.plotting import figure, output_file, ColumnDataSource, show
from bokeh.models import HoverTool, BoxAnnotation, BoxSelectTool, BoxZoomTool, WheelZoomTool, ResetTool

df = pd.read_csv(r"C:\Users\paul.shapiro\Documents\kwdata.csv", parse_dates=['Interest over time_time'])
df.rename(columns={'Search Term': 'keyword', 'Interest over time_time': 'date', 'Weekly Volume': 'volume'}, inplace=True)

gp = df.groupby('volume')

source = ColumnDataSource(data=dict(x=df['date'], y=df['volume'], desc=df['keyword']))
TOOLS = [HoverTool(tooltips=[("Keyword", "@desc"),("Date", "@x"),("Search Volume", "@y")]), BoxZoomTool(), WheelZoomTool(), ResetTool()]

p = figure(plot_width=800, plot_height=800, tools=TOOLS, x_axis_type="datetime")

gp = df.groupby('keyword')
# groups() returns a dict with 'Gene':indices as k:v pair
for g in gp.groups.items():
p.multi_line(xs=[df.loc[g[1], 'date']], ys=[df.loc[g[1], 'volume']])

p.circle(df['date'], df['volume'], fill_color="white", size=8, source=source)

output_file("newline.html")
show(p)

最佳答案

我看不出您的代码有任何问题。根据 Bokeh 示例,尝试查看数据帧 df 与简单的嵌套值列表有何不同。也许通过对数据框进行一些操作,您可以使它正常工作。

http://docs.bokeh.org/en/latest/docs/reference/plotting.html

from bokeh.plotting import figure, output_file, show

p = figure(plot_width=300, plot_height=300)
p.multi_line(xs=[[1, 2, 3], [2, 3, 4]], ys=[[6, 7, 2], [4, 5, 7]],
color=['red','green'])

show(p)

关于python - 在 Bokeh 中,奇怪的日期轴问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35348707/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com