简体   繁体   English

带标签的Geopandas图

[英]Geopandas Plot with label

I have a geopandas file: 我有一个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]
)

Now, I would like to plot it, but depending on the name "A", "B" ist should either be plotted in red or in black. 现在,我想绘制它,但是根据名称“ A”,“ B” ist应该以红色或黑色绘制。 My real dataset is much much larger. 我的真实数据集要大得多。 Therefore, a efficient solution would be highly appreciated. 因此,将高度赞赏有效的解决方案。 Thanks! 谢谢!

You could add a column with a numeric equivalent and define your own cmap. 您可以添加一列等价的数字并定义自己的cmap。 You can use 0 and 1 for A and B or 0, 0.5 and 1 for A, B and C and so on. 您可以将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