簡體   English   中英

帶標簽的Geopandas圖

[英]Geopandas Plot with label

我有一個geopandas文件:

from shapely.geometry import Point, LineString
import geopandas

line1 = LineString([
    Point(0, 0),
    Point(0, 1),
    Point(1, 1),
    Point(1, 2),
    Point(3, 3),
    Point(5, 6),
])

line2 = LineString([
    Point(5, 3),
    Point(5, 5),
    Point(9, 5),
    Point(10, 7),
    Point(11, 8),
    Point(12, 12),
])

line3 = LineString([
    Point(9, 10),
    Point(10, 14),
    Point(11, 12),
    Point(12, 15),
])

gdf = geopandas.GeoDataFrame(
    data={'name': ['A', 'B', 'A']},
    geometry=[line1, line2, line3]
)

現在,我想繪制它,但是根據名稱“ A”,“ B” ist應該以紅色或黑色繪制。 我的真實數據集要大得多。 因此,將高度贊賞有效的解決方案。 謝謝!

您可以添加一列等價的數字並定義自己的cmap。 您可以將0和1用於A和B,或將0、0.5和1用於A,B和C,依此類推。

from shapely.geometry import Point, LineString
import geopandas
from matplotlib.colors import LinearSegmentedColormap
from matplotlib import pyplot as plt


line1 = LineString([
    Point(0, 0), Point(0, 1), Point(1, 1), Point(1, 2),
    Point(3, 3), Point(5, 6),])

line2 = LineString([
    Point(5, 3), Point(5, 5), Point(9, 5), Point(10, 7),
    Point(11, 8), Point(12, 12),])

line3 = LineString([
    Point(9, 10), Point(10, 14), Point(11, 12), Point(12, 15),])

gdf = geopandas.GeoDataFrame(
    data={'name': ['A', 'B', 'A']},
    geometry=[line1, line2, line3]
)

my_cmap = LinearSegmentedColormap.from_list(
    'mycmap', [(0, 'red'), (1, '#000000')])

gdf['num'] = gdf['name'].replace({'A': 0, 'B': 1})

gdf.plot(cmap=my_cmap, column='num')
plt.show()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM