[英]Bokeh not displaying plot for pandas
I can't get Bokeh to display my plot. 我无法让Bokeh显示我的情节。 This is my Python code.
这是我的Python代码。
import pandas as pd
from bokeh.plotting import figure, ColumnDataSource
from bokeh.io import output_file, show
if __name__ == '__main__':
file = 'Overview Data.csv'
overview_df = pd.read_csv(file)
overview_ds = ColumnDataSource(overview_df)
output_file('Wins across Seasons.html')
print(overview_ds.data)
p = figure(plot_width=400, plot_height=400)
# add a circle renderer with a size, color, and alpha
p.circle('Season', 'Wins', source = overview_ds, size=20, color="navy", alpha=0.5)
# show the results
show(p)
I checked my Chrome browser Inspect Element and the console shows the following. 我检查了我的Chrome浏览器的Inspect Element,控制台显示以下内容。
Wins across Seasons.html:17 [bokeh] could not set initial ranges e.set_initial_range @ Wins across Seasons.html:17
在Seasons.html:17上获胜[散景]无法设置初始范围e.set_initial_range @在Seasons.html:17上获胜
This only seems to happen when I am reading from a file. 这似乎仅在我从文件读取时发生。 Hard-coding x and y coordinates work.
硬编码x和y坐标有效。
I have checked other posts but none of the fixes worked. 我检查了其他帖子,但没有修复程序。 All my packages are up to date.
我所有的包裹都是最新的。
This is the file I am reading 这是我正在阅读的文件
Season,Matches Played,Wins,Losses,Goals,Goals Conceded,Clean Sheets
2011-12,38,28,5,89,33,20
2010-11,38,23,4,78,37,15
2009-10,38,27,7,86,28,19
2008-09,38,28,4,68,24,24
2007-08,38,27,5,80,22,21
2006-07,38,28,5,83,27,16
This is the output of the print statement. 这是print语句的输出。
{'Season': array(['2011-12', '2010-11', '2009-10', '2008-09', '2007-08', '2006-07'],
dtype=object), 'Matches Played': array([38, 38, 38, 38, 38, 38], dtype=int64), 'Wins': array([28, 23, 27, 28, 27, 28], dtype=int64), 'Losses': array([5, 4, 7, 4, 5, 5], dtype=int64), 'Goals': array([89, 78, 86, 68, 80, 83], dtype=int64), 'Goals Conceded': array([33, 37, 28, 24, 22, 27], dtype=int64), 'Clean Sheets': array([20, 15, 19, 24, 21, 16], dtype=int64), 'index': array([0, 1, 2, 3, 4, 5], dtype=int64)}
Bokeh does not know what to do with those string dates unless you tell it. 除非您告诉Bokeh,否则不知道如何处理这些字符串日期。 There are two basic possibilities:
有两种基本可能性:
Keep them as strings, and treat them as categorical factors. 将其保留为字符串,并将其视为分类因素。 You can do that by telling Bokeh what the factors are when you create the plot:
您可以通过告诉Bokeh创建图时的因素来做到这一点:
p = figure(plot_width=400, plot_height=400, x_range=list(overview_df.Season.unique()))
That results in this figure: 结果如下图所示:
If you want a different order of categories you can re-order x_range
however you like. 如果您希望类别的顺序不同,则可以根据需要重新排序
x_range
。
Convert them to real datetime values and use a datetime
axis. 将它们转换为实际的日期时间值,并使用
datetime
时间轴。 You can do this by telling Pandas to parse column 0 as a date field: 您可以通过告诉Pandas将列0解析为日期字段来做到这一点:
overview_df = pd.read_csv(file, parse_dates=[0])
and telling Bokeh to use a datetime axis: 并告诉Bokeh使用日期时间轴:
p = figure(plot_width=400, plot_height=400, x_axis_type="datetime")
That results in this figure: 结果如下图所示:
you can convert the 'Season'-column to datetime to get an output. 您可以将“季节”列转换为日期时间以获取输出。
overview_df = pd.read_csv(file)
overview_df.Season = pd.to_datetime(overview_df.Season)
overview_ds = ColumnDataSource(overview_df)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.