Search code examples
pythonbokeh

Bokeh - Vertical layout of plots


I am trying to show two plots vertically. I get an error message at show(column(west_fig, east_fig))

Could you please help me understand the error message?

Version: BokehJS 1.4.0 successfully loaded.

Note: I am running this code in Jupyter notebook

## Detailed error message:

--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) in 8 9 # Plot the two visualizations in a vertical configuration ---> 10 show(column(west_fig, east_fig))

RuntimeError: Models must be owned by only a single document, DaysTicker(id='2330', ...) is already in a doc

# Bokeh libraries
from bokeh.io import output_file, output_notebook
from bokeh.plotting import figure, show
from bokeh.layouts import column

# output to notebook
output_notebook()

# Plot the two visualizations in a vertical configuration
show(column(west_fig, east_fig))

####### west_fig #######

# Bokeh libraries
from bokeh.plotting import figure, show
from bokeh.io import output_file, output_notebook
from bokeh.models import ColumnDataSource, CDSView, GroupFilter

# Output to notebook
output_notebook()

# Convert `stDate` column to datetime column
west_top_2['stDate'] = pd.to_datetime(west_top_2['stDate'], format = '%Y-%m-%d')


# Create a ColumnDataSource
west_cds = ColumnDataSource(west_top_2)

# Create views for each team
rockets_view = CDSView(source = west_cds,
                       filters = [GroupFilter(column_name = 'teamAbbr', group = 'HOU')]   
                      )
warriors_view = CDSView(source = west_cds,
                        filters = [GroupFilter(column_name = 'teamAbbr', group = 'GS')]
                       )

# Create and configure the figure
west_fig = figure(x_axis_type = 'datetime',
                  plot_height = 500,
                  plot_width = 600,
                  title = 'Western Conference Top 2 Teams Wins Race, 2017-18',
                  x_axis_label = 'Date',
                  y_axis_label = 'Wins',
                  toolbar_location = None
                 )

# Render the race as step lines
west_fig.step('stDate', 'gameWon', source = west_cds, view = rockets_view, color = '#CE1141', legend = 'Rockets')
west_fig.step('stDate', 'gameWon', source = west_cds, view = warriors_view, color = '#006BB6', legend = 'Warriors')

# Move the legend to the upper top-left corner
west_fig.legend.location = "top_left"

# Show the plot
show(west_fig)

####### east_fig #########

import pandas as pd

# Bokeh libraries
from bokeh.plotting import figure, show
from bokeh.io import output_file, output_notebook
from bokeh.models import ColumnDataSource, CDSView, GroupFilter


# Output to notebook
output_notebook()

# Convert stDate to datetime column
standings['stDate'] = pd.to_datetime(standings['stDate'], format = '%Y-%m-%d')

# Create a ColumnDataSource
standings_cds = ColumnDataSource(standings)

# Create views for each team
celtics_view = CDSView(source=standings_cds,
                       filters = [GroupFilter(column_name = 'teamAbbr', group = 'BOS')]
                      )
raptors_view = CDSView(source=standings_cds,
                       filters = [GroupFilter(column_name = 'teamAbbr', group = 'TOR')]
                      )

# Configure Figure object
east_fig = figure(x_axis_type = 'datetime',
                  plot_height = 500,
                  plot_width = 600,
                  title = 'Eastern Conference Top 2 Teams Wins Race, 2017-18',
                  x_axis_label = 'Date',
                  y_axis_label = 'Wins',
                  toolbar_location = None
                 )

# Render the race as step lines
east_fig.step('stDate', 'gameWon', color = '#007A33', legend = 'Celtics', 
               source = standings_cds, view = celtics_view)
east_fig.step('stDate', 'gameWon', color = '#CE1141', legend = 'Raptors',
               source = standings_cds, view = raptors_view)

# Move the legend to the upper left hand corner
east_fig.legend.location = "top_left"

# Show the plot
show(east_fig)

Solution

  • Make only one show statement at the end of your code. This implies deleting show(column(west_fig, east_fig)), show(west_fig), show(east_fig) lines and adding at the end of the code show(column(west_fig, east_fig))