简体   繁体   English

无法使用python显示散景图

[英]Cannot get Bokeh graph to show using Python

I'm pretty new to Python and this is my first time using Bokeh. 我刚接触Python,这是我第一次使用Bokeh。 I've followed a tutorial using NFL data to show graphs and I cannot get the graph to show on my machine. 我遵循了使用NFL数据显示图形的教程,但无法在计算机上显示图形。 The script runs without error, but nothing shows. 该脚本运行无错误,但未显示任何内容。 I'm sure I'm missing something very simple... but I just don't know what that is... Can someone please help me? 我确定我缺少一些非常简单的内容...但是我只是不知道那是什么...有人可以帮助我吗? Below is my code: 下面是我的代码:

import pandas as pd
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, FactorRange, FixedTicker
from bokeh.io import output_notebook
from collections import Counter
from bokeh.transform import factor_cmap
from bokeh.palettes import Paired, Spectral
import itertools
pd.set_option('display.max_columns', 150)
output_notebook()

filename = '/Users/ksilva/Downloads/NFL Play by Play 2009-2017 (v4).csv'
df = pd.read_csv(filename,dtype={25: object, 51: object})

# print(df.shape)

# df['down'].isnull().sum()
pd.to_numeric(df['down'], errors='coerce').isnull().sum()

# print(df.loc[51])

# filter by team if desired
team = 'all'
if team == 'all':
    team_df = df
else:
    team_df = df.loc[df['posteam'] == team]

# drop rows will null in the 'down' column
team_df = team_df.loc[df['down'].notnull()]

all_play_types = Counter(team_df['PlayType'])

# print(team_df)
# print(all_play_types)

# list of downs I care about
downs = ['1','2','3','4']

# list of plays I care about
plays = ['Pass', 'Run', 'Punt', 'Field Goal']

# define x-axis categories to be used in the vbar plot
x = list(itertools.product(downs, plays))
# x = [('1', 'Pass'), ('1', 'Run'), ('1', 'Punt'), ..., ('4', 'Punt'), ('4', 'Field Goal')]

# create a list of Counters for each down--will include ALL PlayTypes for each down
plays_on_down = [Counter(team_df.loc[team_df['down'] == int(down)]['PlayType']) for down in downs]

# create a list of counts for each play in plays for each down in downs
counts = [plays_on_down[int(down)-1][play] for down, play in x]

# load the into the ColumnDataSource
source = ColumnDataSource(data=dict(x=x, counts=counts))

# get the figure ready
p = figure(x_range=FactorRange(*x), plot_height=350, plot_width=750, title='Play by Down',
       toolbar_location=None, tools='')

# create the vbar
p.vbar(x='x', top='counts', width=0.9, source=source, line_color='white',
       fill_color=factor_cmap('x', palette=Spectral[4], factors=plays, start=1, end=2))
p.y_range.start = 0
p.x_range.range_padding = 0.1
p.xaxis.major_label_orientation = 1
p.xaxis.axis_label = 'Down'
p.yaxis.axis_label = 'Number of Plays'
p.xgrid.grid_line_color = None
show(p)

For whatever reason, nothing happens when executed from the terminal. 无论出于何种原因,从终端执行时都不会发生任何事情。

Any help is greatly appreciated! 任何帮助是极大的赞赏!

Thanks. 谢谢。

You are setting calling output_notebook . 您正在设置调用output_notebook This activates a mode that only diplays in a Jupyter notebook. 这将激活在Jupyter笔记本电脑中显示的模式。 If you want to execute plain python scripts to generate HTML file output, you should use output_file . 如果要执行普通的python脚本以生成HTML文件输出,则应使用output_file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM