Geopandas情节与标签

时间:2018-01-03 20:02:51

标签: python plot geopandas

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

1 个答案:

答案 0 :(得分:0)

您可以添加一个具有等效数字的列,并定义您自己的cmap。 A和B可以使用0和1,A,B和C可以使用0,0.5和1等。

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