|
1 | | -#!/usr/bin/python3 |
2 | | -# author: Frank Löffler <[email protected]> |
3 | | -# license: CC0 |
4 | | - |
5 | | -import numpy as np |
| 1 | +#!/usr/bin/env python3 |
| 2 | +import sys, argparse |
6 | 3 | import matplotlib.pyplot as plt |
| 4 | +import json |
| 5 | +from pprint import pprint |
7 | 6 |
|
8 | | -# preliminary names |
9 | | -names = ["RSE Network", |
10 | | - "Partner Network", |
11 | | - "RSE Teaching", |
12 | | - "RSE Consultation", |
13 | | - "SW Development", |
14 | | - "SW Maintenance", |
15 | | - "RSE Infrastructure", |
16 | | - "RSE Research", |
17 | | - "RSE Outreach", |
18 | | - ] |
19 | | -# some example values |
20 | | -data = np.array([[1, 0.2], |
21 | | - [3, 0.6,], |
22 | | - [1, 0.1,], |
23 | | - [3, 0.2,], |
24 | | - [4, 0.4,], |
25 | | - [3, 0.3,], |
26 | | - [0, 0.8,], |
27 | | - [2, 0.6,], |
28 | | - [3, 1,], |
29 | | - ]) |
30 | | - |
31 | | -fig = plt.figure(figsize=(10,5)) |
32 | | - |
33 | | -# percentages of the widths of the left and middle section of the plot, with the right |
34 | | -# part being calculated from those (assuming 100% plot width) |
35 | | -widths = [0.2, 0.1] |
36 | | -widths.append(1-sum(widths)) |
37 | | -ax_l = fig.add_axes((0, 0, widths[0], 1)) |
38 | | -ax_c = fig.add_axes((widths[0], 0, widths[1], 1)) |
39 | | -ax_r = fig.add_axes((widths[0]+widths[1], 0, widths[2], 1)) |
40 | | - |
41 | | -xstep = 0.1 |
42 | | - |
43 | | -# eps: small value to ensure that where-clauses do not leave some range un-plotted |
44 | | -# should roughly be half of the smallest step-size you want in x-direction |
45 | | -eps = xstep/2 |
| 7 | +def errorexit(msg='internal error', code=1): |
| 8 | + print(msg, file=sys.stderr) |
| 9 | + sys.exit(code) |
46 | 10 |
|
47 | | -x = np.arange(0, 1+eps, xstep) |
48 | | -y = [np.repeat(0, len(x))] |
49 | | -p = [np.repeat(0, len(x))] |
50 | | -ye = [[0, 0],] |
51 | | -ysum = np.sum(data[:,0]) |
52 | | -for i in np.arange(len(data)): |
53 | | - y.append(np.repeat(data[i,0], len(x))/ysum + y[-1]) |
54 | | - p.append(np.repeat(data[i,1], len(x))) |
55 | | - ye.append([(i+1)/len(data), y[-1][0]]) |
| 11 | +parser = argparse.ArgumentParser( |
| 12 | + description="Plot activity fields of different institutions", add_help=False) |
| 13 | +parser.add_argument('infiles', metavar='infile', nargs="+", |
| 14 | + help="json files containing compositions of individual institutions") |
| 15 | +parser.add_argument('--outfile', default='group_composition_plot.pdf', |
| 16 | + help="specifies output file name, and by extension also the output format") |
| 17 | +parser.add_argument('--legend', action='store_true', |
| 18 | + help="add a legend") |
| 19 | +parser.add_argument('--hide-titles', action='store_true', |
| 20 | + help="hide the pie plot titles") |
| 21 | +args = vars(parser.parse_args()) |
56 | 22 |
|
57 | | -for i in np.arange(len(data))+1: |
58 | | - ax_l.fill_between([0,1], [ye[i][0], ye[i][0]], facecolor="none", edgecolor='k') |
59 | | - ax_l.text(0.05, (ye[i][0]+ye[i-1][0])/2, names[i-1]) |
| 23 | +data = {} |
| 24 | +activity_names = None |
| 25 | +for infilename in args['infiles']: |
| 26 | + try: |
| 27 | + infile = open(infilename, 'r') |
| 28 | + except: |
| 29 | + errorexit(f'Error: cannot read file {infilename}') |
| 30 | + try: |
| 31 | + data[infilename] = json.loads(infile.read()) |
| 32 | + except: |
| 33 | + errorexit(f'Error: cannot parse data in file {infilename}') |
| 34 | + if activity_names and data[infilename]['activity_names'] != activity_names: |
| 35 | + errorexit(f'Error: inconsitent activity names between {infilename} and others in {data.keys()}') |
| 36 | + else: |
| 37 | + activity_names = data[infilename]['activity_names'] |
60 | 38 |
|
61 | | - ax_c.fill_between([0,1], ye[i], facecolor="none", edgecolor='k') |
| 39 | +colors = plt.cm.Paired(range(len(activity_names))) |
62 | 40 |
|
63 | | - ax_r.fill_between(x, y[i-1], y[i], facecolor="none", edgecolor='k', hatch='/', where=p[i]>x-eps) |
64 | | - ax_r.fill_between(x, y[i-1], y[i], facecolor="none", edgecolor='k', hatch='\\', where=p[i]<x+eps) |
| 41 | +# Mapping activities to colors |
| 42 | +activity_to_color = {activity: colors[i] for i, activity in enumerate(activity_names)} |
65 | 43 |
|
66 | | -for ax in [ax_l, ax_c, ax_r]: |
67 | | - ax.set_xlim(0,1) |
68 | | - ax.set_ylim(0,1) |
69 | | - ax.set_xticks([]) |
70 | | - ax.set_yticks([]) |
| 44 | +# Creating the joint plot |
| 45 | +scaling = .9 |
| 46 | +iymax = (len(data)+1)//2 |
| 47 | +fig = plt.figure(figsize=(scaling*16, scaling*8*iymax)) |
71 | 48 |
|
| 49 | +i = 1 |
| 50 | +for inst, idata in data.items(): |
| 51 | + ax = fig.add_subplot(iymax, min(2, len(data)), i) |
| 52 | + ax.pie(idata['activity_weights'], colors=colors, startangle=140) |
| 53 | + inst_name = idata['institution_name'] |
| 54 | + if 'group_name' in idata: |
| 55 | + inst_name = idata['group_name'] + "\n" + inst_name |
| 56 | + if not args['hide_titles']: |
| 57 | + ax.set_title(inst_name, fontsize=20) |
| 58 | + i += 1 |
72 | 59 |
|
| 60 | +# Shared legend |
| 61 | +if args['legend']: |
| 62 | + fig.legend(activity_names, title="Activities", loc="center right", bbox_to_anchor=(1.2, 0.5), fontsize=20) |
73 | 63 |
|
74 | | -plt.savefig("group_composition_plot.pdf") |
75 | | -#plt.show() |
| 64 | +plt.tight_layout() |
| 65 | +fig.savefig(args['outfile'], bbox_inches='tight') |
| 66 | +# plt.show() |
0 commit comments