question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Colorbars change plot dimensions

See original GitHub issue

Thanks so much for adding colorbars to Bokeh 0.12.2!

If I take the colorbar example from http://bokeh.pydata.org/en/latest/docs/user_guide/annotations.html and comment out the colorbar, I get a nice square plot:

image

But if I enable the colorbar, it’s all squished:

image

To me, this is very surprising behavior – why would I want my plot to get squished when I add a colorbar? A colorbar is something outside my plot. It seems to be putting the entire plot into a box and then adjusting the internal plot size, which makes sense for making it easier to lay out the rest of the page, but it’s definitely not what I would have wanted. And I don’t see any obvious way to restore my plot to its rightful dimensions, which would appear to require finding out how big the colorbar is somehow.

The code is below; my only change was to specify the size as 200x200, but the default size has the same issue.

import numpy as np
from matplotlib.mlab import bivariate_normal

from bokeh.plotting import figure, output_file, show
from bokeh.models import LogColorMapper, LogTicker, ColorBar

output_file('color_bar.html')

N = 100
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
Z1 = bivariate_normal(X, Y, 0.1, 0.2, 1.0, 1.0) +  \
    0.1 * bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
image = Z1 * 1e6

color_mapper = LogColorMapper(palette="Viridis256", low=1, high=1e7)

plot = figure(x_range=(0,1), y_range=(0,1), height=200, width=200, toolbar_location=None)
plot.image(image=[image], color_mapper=color_mapper,
           dh=[1.0], dw=[1.0], x=[0], y=[0])

color_bar = ColorBar(color_mapper=color_mapper, ticker=LogTicker(),
                     label_standoff=12, border_line_color=None, location=(0,0))

plot.add_layout(color_bar, 'right')

show(plot)

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:6
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
cchwalacommented, Apr 5, 2018

Not a real solution, but at least a workaround that is a bit more pleasant to use than the one from @jbednar because only the width of the dummy figure has to be set to a meaningful value. The “trick” is to specify the figure height (and width) and use this height for all figures. Unfortunately one cannot get the height of an existing figure via figure.height and use it for the dummy figure. That would be easiest. Alternatively one could get the width of the bounding box of the colorbar, but I have not found out how to get that…

Here is my “solution” (a modification of the above on from jbednar, incorporating ideas from this SO answer


import numpy as np
from bokeh.plotting import figure, output_file, show, output_notebook
from bokeh.models import LogColorMapper, LogTicker, ColorBar
from bokeh.palettes import viridis
from bokeh.layouts import row, column

output_notebook()

fig_height = 400
fig_width = 300

Z = np.random.rand(100, 100)

color_mapper = LogColorMapper(palette=viridis(256), 
                              low=0, 
                              high=10)

plot = figure(x_range=(0,1), 
              y_range=(0,1), 
              height=fig_height, 
              width=fig_width, 
              toolbar_location=None)
plot.image(image=[Z], 
           color_mapper=color_mapper,
           dh=[1.0], 
           dw=[1.0], 
           x=[0], 
           y=[0])

color_bar = ColorBar(color_mapper=color_mapper, 
                     border_line_color=None, 
                     location=(0,0))

dummy = figure(height=fig_height, 
               width=120, 
               toolbar_location=None, 
               min_border=0, 
               outline_line_color=None,
               title='colorbar label',
               title_location='right')
dummy.add_layout(color_bar, 'right')
dummy.title.align = 'center'
dummy.title.text_font_size = '10pt'

r = row(plot,dummy)

show(r)gful values

0reactions
mattpapcommented, Nov 5, 2018

By default nothing will change, however, PR #8085 adds support for configuring frame dimensions (instead of plot dimensions), so with some configuration this issue will be solvable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set Matplotlib colorbar size to match graph
This is slightly changing the sizing of the graphs. I have 4 in a 2x2 grid, but only want the two on the...
Read more >
How to change Matplotlib color bar size in Python?
In this article, We are going to change Matplotlib color bar size in Python. ... Method 1: Resizing color-bar using shrink keyword argument....
Read more >
How to decrease colorbar width in Matplotlib?
To decrease colorbar width in Matplotlib, we can use shrink in colorbar() method. Steps. Set the figure size and adjust the padding between ......
Read more >
How to change Matplotlib color bar size in Python - Adam Smith
Call matplotlib.pytplot.imshow(X) with a two-dimensional array X to plot it and return a matplotlib.image.AxesImage object ...
Read more >
matplotlib.pyplot.colorbar — Matplotlib 3.1.2 documentation
This argument is mandatory for the Figure.colorbar method but optional for the ... shrink, 1.0; fraction by which to multiply the size of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found