散景不显示熊猫图

阿卡罗伊

我无法让 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 浏览器 Inspect Element,控制台显示以下内容。

跨季节的胜数.html:17 [bokeh] 无法设置初始范围 e.set_initial_range @ 跨季节的胜数.html:17

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

我检查了其他帖子,但没有一个修复工作。我所有的包裹都是最新的。

这是我正在阅读的文件

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()))
    

    这导致了这个数字:

    在此处输入图片说明

    如果您想要不同的类别顺序,您可以随意重新排序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")
    

    这导致了这个数字:

    在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章