2019-03-31 05:22:00 +00:00
|
|
|
import fire
|
2019-02-17 06:20:20 +00:00
|
|
|
import matplotlib.pyplot as plt
|
2019-03-31 07:29:06 +00:00
|
|
|
from matplotlib.collections import PatchCollection
|
|
|
|
from matplotlib.patches import Circle, Patch
|
2019-05-18 11:47:28 +00:00
|
|
|
from utils import logger, to_range
|
2019-03-31 05:22:00 +00:00
|
|
|
from constants import BIOMES
|
2019-02-28 10:04:47 +00:00
|
|
|
|
2019-02-17 06:20:20 +00:00
|
|
|
import pandas as pd
|
|
|
|
import cartopy.crs as ccrs
|
|
|
|
|
2019-05-22 03:59:42 +00:00
|
|
|
def draw(df, earth=True, width=23.22, height=13, longitude_max=None, latitude_max=None, only_draw=False, path=None, alpha=1):
|
2019-03-31 05:22:00 +00:00
|
|
|
logger.debug('draw(df, %s)', path)
|
2019-02-27 11:36:20 +00:00
|
|
|
biomes = {}
|
|
|
|
biome_numbers = df['biome_num'].unique()
|
2019-02-17 06:20:20 +00:00
|
|
|
|
2019-03-05 07:59:30 +00:00
|
|
|
for i, row in df.iterrows():
|
2019-05-18 11:47:28 +00:00
|
|
|
if earth:
|
|
|
|
p = (row.longitude, row.latitude)
|
|
|
|
else:
|
2019-05-22 03:59:42 +00:00
|
|
|
p = (to_range(-180, 180, 0, longitude_max)(row.longitude), to_range(-90, 90, 0, latitude_max)(row.latitude))
|
2019-05-18 11:47:28 +00:00
|
|
|
|
2019-02-27 11:36:20 +00:00
|
|
|
if row.biome_num in biomes:
|
2019-03-31 07:29:06 +00:00
|
|
|
biomes[row.biome_num].append(p)
|
2019-02-27 11:36:20 +00:00
|
|
|
else:
|
2019-03-31 07:29:06 +00:00
|
|
|
biomes[row.biome_num] = [p]
|
2019-02-17 06:20:20 +00:00
|
|
|
|
2019-05-18 11:47:28 +00:00
|
|
|
if earth:
|
|
|
|
ax = plt.axes(projection=ccrs.PlateCarree())
|
|
|
|
ax.stock_img()
|
|
|
|
else:
|
|
|
|
ax = plt.gca()
|
2019-03-31 05:22:00 +00:00
|
|
|
|
2019-03-31 07:29:06 +00:00
|
|
|
legend_handles = []
|
2019-02-27 11:36:20 +00:00
|
|
|
for n in biome_numbers:
|
2019-03-31 07:29:06 +00:00
|
|
|
color = BIOMES[n]['color']
|
|
|
|
|
|
|
|
patches = [Circle(p, radius=0.4) for p in biomes[n]]
|
|
|
|
collection = PatchCollection(patches, color=color)
|
2019-05-18 17:36:09 +00:00
|
|
|
collection.set_alpha(alpha)
|
2019-03-31 07:29:06 +00:00
|
|
|
|
|
|
|
legend_handles.append(Patch(color=color, label=BIOMES[n]['name']))
|
|
|
|
ax.add_collection(collection)
|
2019-03-31 05:22:00 +00:00
|
|
|
|
2019-03-31 07:29:06 +00:00
|
|
|
ax.legend(handles=legend_handles, loc='center left', bbox_to_anchor=(1, 0.5), markerscale=4)
|
2019-05-18 11:47:28 +00:00
|
|
|
|
2019-03-31 07:29:06 +00:00
|
|
|
ax.autoscale_view()
|
2019-03-31 05:22:00 +00:00
|
|
|
figure = plt.gcf()
|
2019-05-18 11:47:28 +00:00
|
|
|
figure.set_size_inches(width, height)
|
2019-03-31 07:29:06 +00:00
|
|
|
figure.subplots_adjust(left=0.02, right=0.79)
|
2019-05-18 11:47:28 +00:00
|
|
|
|
|
|
|
if only_draw: return
|
|
|
|
|
2019-02-28 10:04:47 +00:00
|
|
|
if path:
|
|
|
|
plt.savefig(path)
|
|
|
|
else:
|
|
|
|
plt.show()
|
2019-02-17 06:20:20 +00:00
|
|
|
|
2019-03-31 05:22:00 +00:00
|
|
|
def draw_cmd(path=None):
|
|
|
|
draw(pd.read_pickle('data.p'), path=path)
|
|
|
|
|
2019-02-27 11:36:20 +00:00
|
|
|
if __name__ == "__main__":
|
2019-03-31 05:22:00 +00:00
|
|
|
fire.Fire(draw_cmd)
|