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.

[BUG] nan_color argument in LinearColorMapper is not used

See original GitHub issue

ALL software version info (bokeh, python, notebook, OS, browser, any other relevant packages)

bokeh.version: 1.3.0 python: 3.7.3 OS: Windows 10 browser: all

Description of expected behavior and the observed behavior

LinearColorMapper has the option to set nan_color. It is however not used. Instead it sets the color of the nan entry to be the lowest color in the range.

In the example below you should see the center region as black, but instead it shows as lowest color in the range.

Complete, minimal, self-contained example code that reproduces the issue

To run, download this file: provinces.zip

import geopandas as gpd 
import numpy as np
from bokeh.palettes import Reds
from bokeh.models import GeoJSONDataSource, LinearColorMapper
from bokeh.plotting import figure, show

geodf = gpd.read_file("provinces.geojson")
geodf.loc[6, 'index'] = np.nan
print(geodf)
color_mapper = LinearColorMapper(palette=Reds[9][::-1], nan_color='black')

p = figure()
geo_source = GeoJSONDataSource(geojson=geodf.to_json())
p.patches('xs', 'ys', fill_alpha=1, 
              fill_color={'field': 'index', 'transform': color_mapper}, 
              line_color='white', line_width=0.5, 
              source=geo_source)

show(p)

Stack traceback and/or browser JavaScript console output

Data frame that is plotted image

Screenshots or screencasts of the bug in action

image

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
bryevdvcommented, Aug 21, 2019

Yes, brilliant this small change fixes:

diff --git a/bokehjs/src/lib/models/sources/geojson_data_source.ts b/bokehjs/src/lib/models/sources/geojson_data_sourc
index a7918a425..661a011d9 100644
--- a/bokehjs/src/lib/models/sources/geojson_data_source.ts
+++ b/bokehjs/src/lib/models/sources/geojson_data_source.ts
@@ -31,6 +31,10 @@ export namespace GeoJSONDataSource {

 export interface GeoJSONDataSource extends GeoJSONDataSource.Attrs {}

+function orNaN(v: number | undefined) {
+  return v != null ? v : NaN
+}
+
 export class GeoJSONDataSource extends ColumnarDataSource {
   properties: GeoJSONDataSource.Props

@@ -75,16 +79,12 @@ export class GeoJSONDataSource extends ColumnarDataSource {
     for (const property in properties) {
       if (!data.hasOwnProperty(property))
         data[property] = this._get_new_nan_array(item_count)
-      data[property][i] = properties[property]
+      data[property][i] = orNaN(properties[property])
     }
   }

   private _add_geometry(geometry: GeoItem, data: GeoData, i: number): void {

-    function orNaN(v: number | undefined) {
-      return v != null ? v : NaN
-    }
-
     function flatten(acc: Position[], item: Position[]) {
       return acc.concat([[NaN, NaN, NaN]]).concat(item)
     }

_add_properties tried to handle this by pre-populating nan arrays for the values but it did not all “orNan” on each value, so nulls that made it over would just overwrite the nans. I will add a PR for this later this week unless someone else gets to it first (needs a unit test that maintains nulls in properties end up as nans in the columns)

0reactions
harmbuismancommented, Aug 21, 2019

@bryevdv, great to read you found a solution, thanks for the quick turnaround!

Read more comments on GitHub >

github_iconTop Results From Across the Web

mappers — Bokeh 3.0.3 Documentation
Color to be used if data is NaN or otherwise not mappable. Acceptable values are: any of the named CSS colors, e.g 'green'...
Read more >
ValueError: Invalid RGBA argument: What is causing this error?
The error message is misleading. You're getting a ValueError because the shape of colors is wrong, not because an RGBA value is invalid....
Read more >
Color Of Nan Changes When Change The Center Value Of Colorbar
LinearColorMapper has the option to set nancolor.It is however not used.Instead it sets the color of the nan entry to be the lowest...
Read more >
Python colormap legend - miocittadino.it
Qualitative colormaps: these mix colors with no particular sequence (e. ... Nan Color - this is the color ParaView will use to paint...
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