world-ecoregion/draw.py

60 lines
1.5 KiB
Python

from shapely.geometry import Point, MultiPoint
from shapely.ops import cascaded_union
import matplotlib.pyplot as plt
import pandas as pd
import cartopy.crs as ccrs
def draw(df, path=None):
biomes = {}
biome_numbers = df['biome_num'].unique()
# biome_names = df['biome_name'].unique()
for i, row in df.iterrows():
p = Point(row.longitude, row.latitude)
if row.biome_num in biomes:
biomes[row.biome_num].append(p)
else:
biomes[row.biome_num] = [p]
ax = plt.axes(projection=ccrs.PlateCarree())
ax.stock_img()
# ax.legend(df['biome_name'].unique())
colors={
0: '#016936',
1: '#B2D127',
2: '#77CC00',
3: '#99C500',
4: '#B6CC00',
5: '#00C5B5',
6: '#EFFF00',
7: '#FFEE00',
8: '#009BFF',
9: '#A0ADBA',
10: '#5C62FF',
11: '#00850F',
12: '#FF9E1F',
13: '#FF1F97'
}
for n in biome_numbers:
biomes[n] = MultiPoint(biomes[n]).buffer(1)
# print(biomes[n])
# legend = biome_names[n]
if not hasattr(biomes[n], '__iter__'):
biomes[n] = [biomes[n]]
ax.add_geometries(biomes[n], ccrs.PlateCarree(), facecolor=colors[n])
# artist.set_label(biome_names[n])
# print(artist.get_label())
# ax.legend(artists, biome_names)
if path:
plt.savefig(path)
else:
plt.show()
if __name__ == "__main__":
df = pd.read_pickle('data.p')
draw(df)