world-ecoregion/biomes/draw.py
2019-04-09 08:20:32 +04:30

52 lines
1.4 KiB
Python

import fire
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
from matplotlib.patches import Circle, Patch
from utils import logger
from constants import BIOMES
import pandas as pd
import cartopy.crs as ccrs
def draw(df, path=None):
logger.debug('draw(df, %s)', path)
biomes = {}
biome_numbers = df['biome_num'].unique()
for i, row in df.iterrows():
p = (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()
legend_handles = []
for n in biome_numbers:
color = BIOMES[n]['color']
patches = [Circle(p, radius=0.4) for p in biomes[n]]
collection = PatchCollection(patches, color=color)
legend_handles.append(Patch(color=color, label=BIOMES[n]['name']))
ax.add_collection(collection)
ax.legend(handles=legend_handles, loc='center left', bbox_to_anchor=(1, 0.5), markerscale=4)
ax.autoscale_view()
figure = plt.gcf()
figure.set_size_inches(23.22, 13)
figure.subplots_adjust(left=0.02, right=0.79)
if path:
plt.savefig(path)
else:
plt.show()
def draw_cmd(path=None):
draw(pd.read_pickle('data.p'), path=path)
if __name__ == "__main__":
fire.Fire(draw_cmd)