散景不显示 pandas 的情节

Bokeh not displaying plot for pandas

我无法让 Bokeh 显示我的情节。这是我的 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)

我检查了我的 Chrome 浏览器检查元素,控制台显示以下内容。

Wins across Seasons.html:17 [bokeh] could not set initial ranges e.set_initial_range @ Wins across Seasons.html:17

这似乎只有在我从文件中读取时才会发生。硬编码 x 和 y 坐标有效。

我检查了其他帖子,但 none 的修复有效。我所有的包都是最新的。

这是我正在阅读的文件

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

这是打印语句的输出。

{'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 不知道如何处理这些字符串日期,除非您告诉它。有两种基本可能性:

  • 将它们保留为字符串,并将它们视为分类因素。您可以通过在创建绘图时告诉 Bokeh 哪些因素来做到这一点:

    p = figure(plot_width=400, plot_height=400, 
               x_range=list(overview_df.Season.unique()))
    

    结果是这个数字:

    如果您想要不同的类别顺序,您可以 re-order x_range 随心所欲。

  • 将它们转换为实际日期时间值并使用 datetime 轴。您可以通过告诉 Pandas 将第 0 列解析为日期字段来执行此操作:

    overview_df = pd.read_csv(file, parse_dates=[0])
    

    并告诉 Bokeh 使用日期时间轴:

    p = figure(plot_width=400, plot_height=400, x_axis_type="datetime")
    

    结果是这个数字:

您可以将 'Season' 列转换为日期时间以获得输出。

overview_df = pd.read_csv(file)
overview_df.Season = pd.to_datetime(overview_df.Season)
overview_ds = ColumnDataSource(overview_df)