[英]In Bokeh, Weird Date Axis Issue
I'm trying to plot some data with Bokeh via pandas.我正在尝试通过 Pandas 用 Bokeh 绘制一些数据。 The x-axis is date, and I can get Bokeh to plot the axis "mostly" correct (the range may be off).
x 轴是日期,我可以让 Bokeh 绘制“大部分”正确的轴(范围可能关闭)。 However, the line it outputs is all over the place.
然而,它输出的线到处都是。
For example:例如:
It looks like maybe it's one big, continuous line?看起来它可能是一条大而连续的线?
Here's my code:这是我的代码:
# 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)
It's also interesting to note, that if you plot it using bokeh.charts (if I did this the tooltips wouldn't work, so it's not an option), it plots fine:还值得注意的是,如果您使用 bokeh.charts 绘制它(如果我这样做,工具提示将不起作用,因此它不是一个选项),它绘制得很好:
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")
Any help would be much appreciated.任何帮助将非常感激。 This has been driving me crazy!
这让我发疯了!
SOLVED using multi_line() and a for loop:使用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)
I cannot see anything wrong with your code.我看不出你的代码有什么问题。 Try to see how different the dataframe df is from a simple nested list of values as per the bokeh example.
根据散景示例,尝试查看数据框 df 与简单的嵌套值列表有何不同。 Maybe by doing some manipulation to the dataframe you can get this working.
也许通过对数据框进行一些操作,您可以使其正常工作。
http://docs.bokeh.org/en/latest/docs/reference/plotting.htmlhttp://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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.