world-ecoregion/draw.py

56 lines
1.4 KiB
Python
Raw Normal View History

2019-02-17 06:20:20 +00:00
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
2019-02-27 11:36:20 +00:00
def draw(df):
biomes = {}
biome_numbers = df['biome_num'].unique()
# biome_names = df['biome_name'].unique()
2019-02-17 06:20:20 +00:00
2019-02-27 11:36:20 +00:00
for (longitude, latitude), row in df.iterrows():
p = Point(longitude, latitude)
if row.biome_num in biomes:
biomes[row.biome_num].append(p)
else:
biomes[row.biome_num] = [p]
2019-02-17 06:20:20 +00:00
2019-02-27 11:36:20 +00:00
ax = plt.axes(projection=ccrs.PlateCarree())
ax.stock_img()
# ax.legend(df['biome_name'].unique())
2019-02-17 06:20:20 +00:00
2019-02-27 11:36:20 +00:00
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'
}
2019-02-17 06:20:20 +00:00
2019-02-27 11:36:20 +00:00
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())
2019-02-17 06:20:20 +00:00
2019-02-27 11:36:20 +00:00
# ax.legend(artists, biome_names)
plt.show()
2019-02-17 06:20:20 +00:00
2019-02-27 11:36:20 +00:00
if __name__ == "__main__":
df = pd.read_pickle('data_final.p')
draw(df)