0

I'm trying to add labels to a hvplot that shows points on a tiled map.

My GeoDataFrame gdf looks like this:

  id                  geometry
8911  POINT (5.79557 53.20121)
8912  POINT (5.76973 53.18031)
8913  POINT (5.78159 53.20088)
8914  POINT (5.75442 53.20394)
8915  POINT (5.76594 53.21173)

Adding these points to a map is easy:

gdf.hvplot(geo=True, tiles=True)

I then tried using labels to plot text labels in a similar way, but that doesn't work.:

gdf.hvplot.labels(geo=True, tiles=True, text='id')

It gives me this error

Traceback (most recent call last):

  File "<ipython-input-138-73570b6a686e>", line 1, in <module>
    centroid_labels = dfl.hvplot.labels(geo=True, tiles=True, text='id')

  File "C:\Users\user\Miniconda3\envs\otp\lib\site-packages\hvplot\plotting\core.py", line 574, in labels
    return self(x, y, text=text, kind='labels', **kwds)

  File "C:\Users\user\Miniconda3\envs\otp\lib\site-packages\hvplot\plotting\core.py", line 79, in __call__
    return self._get_converter(x, y, kind, **kwds)(kind, x, y)

  File "C:\Users\user\Miniconda3\envs\otp\lib\site-packages\hvplot\converter.py", line 1097, in __call__
    obj = method(x, y)

  File "C:\Users\user\Miniconda3\envs\otp\lib\site-packages\hvplot\converter.py", line 1688, in labels
    text = self.kwds.get('text', [c for c in data.columns if c not in (x, y)][0])

IndexError: list index out of range

Adding the x and y options explicity creates a different error:

gdf.hvplot.labels(geo=True, tiles=True, x=gdf.geometry.x, y=gdf.geometry.y, text='id')
Traceback (most recent call last):

  File "<ipython-input-143-0eaefd24cbe6>", line 1, in <module>
    centroid_labels = dfl.hvplot.labels(geo=True, x=dfl.geometry.x, y=dfl.geometry.y, tiles=True, text='id')

  File "C:\Users\user\Miniconda3\envs\otp\lib\site-packages\hvplot\plotting\core.py", line 574, in labels
    return self(x, y, text=text, kind='labels', **kwds)

  File "C:\Users\user\Miniconda3\envs\otp\lib\site-packages\hvplot\plotting\core.py", line 79, in __call__
    return self._get_converter(x, y, kind, **kwds)(kind, x, y)

  File "C:\Users\user\Miniconda3\envs\otp\lib\site-packages\hvplot\plotting\core.py", line 83, in _get_converter
    x = x or params.pop('x', None)

  File "C:\Users\user\Miniconda3\envs\otp\lib\site-packages\pandas\core\generic.py", line 1442, in __nonzero__
    raise ValueError(

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Saaru Lindestøkke
  • 2,067
  • 1
  • 25
  • 51

1 Answers1

1

I was able to get this to work by converting the CRS to EPSG:3857 (WGS 84 / Pseudo-Mercator) which may or may not be appropriate for your purposes.

As a side note, it would be nice to have a MRE on questions like this.

Here is what worked:

import geopandas as gpd
import pandas as pd
import hvplot.pandas
import geoviews as gv
from geoviews import tile_sources as gvts

raw = """8911  POINT (5.79557 53.20121)
8912  POINT (5.76973 53.18031)
8913  POINT (5.78159 53.20088)
8914  POINT (5.75442 53.20394)
8915  POINT (5.76594 53.21173)"""

data = [(_[0], _[-2], _[-1]) for _ in [_.replace("(", "").replace(")", "").split() for _ in raw.split("\n")]]
df = pd.DataFrame(data, columns=["labels", "x", "y"]).astype({"labels": str})
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.x, df.y, crs="EPSG:4326"))

tiles = gvts.CartoLight
points = gdf.hvplot(geo=True, tiles="CartoLight")
labels = gdf.to_crs("EPSG:3857").assign(x=lambda df: df.geometry.x, y=lambda df: df.geometry.y).hvplot.labels(text="labels", x="x", y="y")
(points * labels * tiles).opts(height=600, width=800)

I would think that this should have worked:

gdf.hvplot.labels(text="labels", geo=True)

This really just works around the original issue, it may be worth reporting the issue on github.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Brian Larsen
  • 612
  • 8
  • 9