Skip to content

Commit

Permalink
Merge pull request #25 from wwymak/colorbar-formatting
Browse files Browse the repository at this point in the history
Colorbar tick formatting
  • Loading branch information
PatrikHlobil authored May 30, 2019
2 parents 00a2576 + c77a23b commit e0e8427
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
6 changes: 5 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@

[munehiro-k](https://github.com/munehiro-k)

* Pull Request: *escape column names in default hovertool_string of stacked bar plots*
* Pull Request: *escape column names in default hovertool_string of stacked bar plots*

[wwymak](https://github.com/wwymak)

* Pull Request: *add option to customize colorbar tick formatting*
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,55 @@ In a similar way, also GeoDataFrames with (multi)line shapes can be drawn using

<br>

---

### Colorbar formatting:

If you want to display the numerical labels on your colorbar with an alternative to the scientific format, you
can pass in a one of the [bokeh number string formats](https://bokeh.pydata.org/en/latest/_modules/bokeh/models/formatters.html#NumeralTickFormatter)
or an instance of one of the [bokeh.models.formatters](https://bokeh.pydata.org/en/latest/docs/reference/models/formatters.html) to the `colorbar_tick_format` argument
in the geoplot

An example of using the string format argument:

```python
df_states = gpd.read_file(r"https://raw.githubusercontent.com/PatrikHlobil/Pandas-Bokeh/master/Documentation/Testdata/states/states.geojson")

df_states["STATE_NAME_SMALL"] = df_states["STATE_NAME"].str.lower()

# pass in a string format to colorbar_tick_format to display the ticks as 10m rather than 1e7
df_states.plot_bokeh(
figsize=(900, 600),
category="POPESTIMATE2017",
simplify_shapes=5000,
colormap="Inferno",
colormap_uselog=True,
colorbar_tick_format="0.0a")
```
![colorbar_tick_format with string argument](Documentation/Images/geoplot_string_tickformatter.png)

An example of using the bokeh `PrintfTickFormatter`:

```python
df_states = gpd.read_file(r"https://raw.githubusercontent.com/PatrikHlobil/Pandas-Bokeh/master/Documentation/Testdata/states/states.geojson")

df_states["STATE_NAME_SMALL"] = df_states["STATE_NAME"].str.lower()

for i in range(8):
df_states["Delta_Population_201%d"%i] = ((df_states["POPESTIMATE201%d"%i] / df_states["POPESTIMATE2010"]) -1 ) * 100

# pass in a PrintfTickFormatter instance colorbar_tick_format to display the ticks with 2 decimal places
df_states.plot_bokeh(
figsize=(900, 600),
category="Delta_Population_2017",
simplify_shapes=5000,
colormap="Inferno",
colorbar_tick_format=PrintfTickFormatter(format="%4.2f"))
```
![colorbar_tick_format with bokeh.models.formatter_instance](Documentation/Images/geoplot_PrintfTickFormatter.png)

<br>

<p id="Layouts"></p>

## Outputs, Formatting & Layouts
Expand Down
19 changes: 19 additions & 0 deletions pandas_bokeh/geoplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

from bokeh.colors import RGB
import bokeh
from bokeh.models import (
TickFormatter,
NumeralTickFormatter
)

blue_colormap = [RGB(255 - i, 255 - i, 255) for i in range(256)]

Expand Down Expand Up @@ -143,6 +147,17 @@ def convert_geoDataFrame_to_patches(gdf, geometry_column_name="geometry"):
return gdf_new


def get_tick_formatter(formatter_arg):

if issubclass(formatter_arg.__class__, TickFormatter):
return formatter_arg
elif isinstance(formatter_arg, str):
return NumeralTickFormatter(format=formatter_arg)
else:
raise ValueError(
"<colorbar_tick_format> parameter only accepts a string or a objects of bokeh.models.formatters.")


def geoplot(
gdf_in,
figure=None,
Expand All @@ -162,6 +177,7 @@ def geoplot(
slider_range=None,
slider_name="",
show_colorbar=True,
colorbar_tick_format=None,
xrange=None,
yrange=None,
hovertool=True,
Expand Down Expand Up @@ -676,6 +692,9 @@ def geoplot(
if colormap_uselog:
colorbar_options["ticker"] = LogTicker()

if colorbar_tick_format:
colorbar_options["formatter"] = get_tick_formatter(colorbar_tick_format)

colorbar = ColorBar(**colorbar_options)

p.add_layout(colorbar, "right")
Expand Down

0 comments on commit e0e8427

Please sign in to comment.