Search code examples
pythonbokehrectglyph

Python Bokeh: Rect not rendering with another glyph


Below I have some code for a circle and rectangle on a plot.

from bokeh.plotting import figure, show
from bokeh.models import LinearAxis, Range1d
from bokeh.resources import settings
from bokeh.models import Legend
from bokeh.io import export_svg



def ExScore(Xax,Yax):

  #X and Y axes
  xfactors = ['-100', '0', '100', '200', '300', '400' ]
  yfactors = ['-100', '-85', '-50', '50', '200', '400']

  #Coordinates for the heatmap rectangles. It should be a grid of 3 across and 5 high
  xr = ['-16.67','-16.67','-16.67','-16.67','-16.67','249.99','249.99','249.99','249.99','249.99','416.65','416.65','416.65','416.65','416.65']
  yr = ['-92.5','-67.5','0','125','300','-92.5','-67.5','0','125','300','-92.5','-67.5','0','125','300']

  #Colors for the rectangles
  colors = [
  '#2ECC71','#82E0AA','#D5F5E3',
  '#2ECC71','#82E0AA','#D5F5E3',
  '#2ECC71','#82E0AA','#D5F5E3',
  '#2ECC71','#82E0AA','#D5F5E3',
  '#2ECC71','#82E0AA','#D5F5E3',
  ]

  #Creating figure
  p = figure(title="ExampleHeatMap", tools="hover", toolbar_location=None,
       x_range=xfactors, y_range=yfactors, plot_width = 500, plot_height = 500)

  #Making axes invisible
  p.xgrid.visible = False
  p.ygrid.visible = False

  #Rendering rectangles that won't show
  p.rect(xr,yr, fill_color=colors, line_color='white', width=100, height=100, fill_alpha=1.0)

  #Rendering circle that does show
  f1 = p.circle(Xax, Yax, size=10, color='#000000', fill_alpha=1.0)

  #Adding legend
  legend = Legend(items=[
      ("Example" , [f1])
  ])
  p.add_layout(legend, 'right')

  show(p)


x = []
y = []

x.append('100')

y.append('50')

ExScore(x,y)

I have gone through some of the other questions here (correcting the number of coordinates and accuracy, adjusting width/height/color, trying to use output_notebook, ensuring plot points and data are uniform [I.e strings], etc. ). No matter what change I make, I can't get the heatmap to render beneath the circle (or at all), rendering a graph like below:

enter image description here

I've tried testing as I know how, but for whatever reason the rect won't render. It's probably something simple that I'm missing, but trying out other solutions in SO hasn't helped yet. I'm using bokeh version 2.4.2. If someone could provide direction, I'd be appreciative.


Solution

  • Please check if this is what you want:

    def ExScore(Xax,Yax):
        xfactors = [-100.0, 0.0, 100.0, 200.0, 300.0, 400.0]
        yfactors = [-100.0, -85.0, -50.0, 50.0, 200.0, 400.0]
    
        #Coordinates for the heatmap rectangles. It should be a grid of 3 across and 5 high
        xr = [-16.67, -16.67, -16.67, -16.67, -16.67, 249.99, 249.99, 249.99, 249.99, 249.99, 416.65, 416.65, 416.65, 416.65, 416.65]
        yr = [-92.5, -67.5, 0.0, 125.0, 300.0, -92.5, -67.5, 0.0, 125.0, 300.0, -92.5, -67.5, 0.0, 125.0, 300.0]
    
        #Colors for the rectangles
        colors = [
        '#2ECC71','#82E0AA','#D5F5E3',
        '#2ECC71','#82E0AA','#D5F5E3',
        '#2ECC71','#82E0AA','#D5F5E3',
        '#2ECC71','#82E0AA','#D5F5E3',
        '#2ECC71','#82E0AA','#D5F5E3',
        ]
    
        #Creating figure
        p = figure(title="ExampleHeatMap", tools="hover", toolbar_location=None,
           plot_width = 500, plot_height = 500)
    
        #Making axes invisible
        p.xgrid.visible = False
        p.ygrid.visible = False
    
        #Rendering rectangles that won't show
        p.rect(xr,yr, fill_color=colors, line_color='white', width=100, height=100, fill_alpha=1.0)
    
        #Rendering circle that does show
        f1 = p.circle(Xax, Yax, size=10, color='#000000', fill_alpha=1.0)
    
        #Adding legend
        legend = Legend(items=[
          ("Example" , [f1])
        ])
        p.add_layout(legend, 'right')
    
        show(p)
    

    The genereated figure looks like this: Heatmap