repl.it
@adriculous/

animal-conservation-mosaicplot

Python

Mosaic plot for selected animals under the Endangered Species or lower categories

fork
loading
Files
  • main.py
  • data.json
  • endspec_mosaic.png
  • Packager files
  • poetry.lock
  • pyproject.toml
  • requirements.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import pandas as pd
import matplotlib.pyplot as plt
import re
import json
from statsmodels.graphics.mosaicplot import mosaic

with open("data.json", "r") as text:
  data = json.load(text)

for item in data:
    item["Category"] = re.compile(" [\.(]").split(item["Category"])[0]

# print(data)

classes = ["Mammalia", "Aves", "Reptilia"]
statuses = ["Endangered", "Critically endangered", "Vulnerable"]

mosaic_data = []
for item in data:
  if item["Animal Class"] in classes and item["Category"] in statuses:
    mosaic_data.append(item)

properties = {
  "Endangered": {"color": "#FACDB6"},
  "Critically endangered": {"color": "#C5CADE"},
  "Vulnerable": {"color": "#A8DBD2"},
}

plt.rc("font", size=8)

mosaic_dataframe = pd.DataFrame(mosaic_data)

fig = mosaic(
  mosaic_dataframe, ["Category", "Animal Class"],
  title="Conservation Status by Animal Class", 
  gap=[0.02,0.02], axes_label=True,
  properties=lambda x: properties[x[0]]
  )

plt.savefig("endspec_mosaic.png")


Fetching token
?