如何在地理 hvplot 点图上叠加文本标签?
How can I overlay text labels on a geographic hvplot points plot?
我正在尝试向在平铺地图上显示点的 hvplot 添加标签。
我的 GeoDataFrame gdf
看起来像这样:
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)
然后我尝试使用 labels 以类似的方式绘制文本标签,但这不起作用。:
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
添加 x
和 y
选项会显式产生不同的错误:
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().
我能够通过将 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 上报告这个问题。
我正在尝试向在平铺地图上显示点的 hvplot 添加标签。
我的 GeoDataFrame gdf
看起来像这样:
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)
然后我尝试使用 labels 以类似的方式绘制文本标签,但这不起作用。:
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
添加 x
和 y
选项会显式产生不同的错误:
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().
我能够通过将 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 上报告这个问题。