【问题标题】:Geopandas Plot with label带标签的 Geopandas 绘图
【发布时间】:2018-04-20 04:26:23
【问题描述】:

我有一个 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”应该用红色或黑色绘制。我的真实数据集要大得多。因此,一个有效的解决方案将受到高度赞赏。谢谢!

【问题讨论】:

  • 不是很清楚你的意思。是要依赖name还是要取消name?

标签: python plot geopandas


【解决方案1】:

您可以添加一个具有等效数字的列并定义您自己的 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()

【讨论】:

    猜你喜欢
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 2021-04-01
    • 2013-05-04
    • 2019-09-05
    相关资源
    最近更新 更多