0

我正在尝试将标签添加到显示平铺地图上的点的 hvplot 中。

我的 GeoDataFramegdf如下所示:

  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)

将这些点添加到地图很容易:

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

然后我尝试使用标签以类似的方式绘制文本标签,但这不起作用。:

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

它给了我这个错误

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

显式添加xandy选项会产生不同的错误:

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().
4

1 回答 1

1

我能够通过将 CRS 转换为EPSG:3857 ( WGS 84 / Pseudo-Mercator ) 来实现这一点,这可能适合也可能不适合您的目的。

附带说明一下,如果有一个关于此类问题的MRE,那就太好了。

这是有效的:

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)

我认为这应该有效:

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

这真的只适用于原始问题,可能值得在 github 上报告该问题。

于 2021-04-21T17:35:43.460 回答