我正在尝试将标签添加到显示平铺地图上的点的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().发布于 2021-04-22 01:35:43
通过将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上报告这个问题可能是值得的。
https://stackoverflow.com/questions/67005004
复制相似问题