Skip to content
This repository was archived by the owner on Feb 22, 2019. It is now read-only.

Commit 7342378

Browse files
committed
Updated
1 parent 3d0deb8 commit 7342378

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

0_sample.png

10.7 KB
Loading

1_sample.png

12.2 KB
Loading

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,30 @@ clusters:
4242

4343
The number of cluster labels must be consistent with the number of items in the clusters sequence, and the number of bar labels must be consistent with the number of elements in each sub-sequence (each cluster).
4444

45-
See the sample.yaml
45+
Running
46+
```
47+
python generate-graph.py sample.yaml sample.png
48+
```
49+
produces `0_sample.png` and `1_sample.png`
4650

4751
Output
4852
------
49-
A pdf generated by matplotlib.pyplot
53+
A graph generated by matplotlib.pyplot. Depending on the extension of the base filename you pass in, you could get any of these file types supported by matplotlib:
54+
55+
* eps
56+
* jpeg, jpg
57+
* pdf
58+
* pgf
59+
* png
60+
* ps
61+
* raw
62+
* rgba
63+
* svg
64+
* svgz
65+
* tif, tiff
5066

5167
Usage
5268
-----
5369
```
54-
./generate-graph.py -i <yaml-filename>
70+
./generate-graph.py -i <yaml-filename> <base_filename>
5571
```

generate-graph.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,13 @@
22

33
# Special thanks to Ellis Michael for YAML parsing code
44

5-
import argparse
5+
from sys import argv, exit
66
import yaml
77
from matplotlib import cm
88
from matplotlib import pyplot as plt
99

1010
from pylab import *
1111

12-
#########################################
13-
## PARSE COMMAND LINE ARGS
14-
#########################################
15-
16-
def parse_args():
17-
parser = argparse.ArgumentParser()
18-
parser.add_argument('-i', '--input',
19-
type=argparse.FileType('r'), required=True)
20-
21-
return vars(parser.parse_args())
22-
2312
#########################################
2413
## PARSE DATA
2514
#########################################
@@ -43,8 +32,7 @@ def getBars(clusters,i):
4332

4433
return bars
4534

46-
def graph(g):
47-
35+
def graph(g, out_file_base, graph_num, num_graphs):
4836
# extract graph info
4937
title = g['title']
5038
y_label = g['y-axis']
@@ -53,7 +41,11 @@ def graph(g):
5341
cluster_labels = g['cluster-labels'] # each cluster
5442
clusters = g['clusters'] # the actual data
5543

44+
overlap = False
45+
5646
# create a figure
47+
#fig, ax = plt.subplots(figsize=(100, 5))
48+
#fig, ax = plt.subplots(figsize=(15, 5))
5749
fig, ax = plt.subplots()
5850

5951
# max width of bar
@@ -65,11 +57,15 @@ def graph(g):
6557

6658
# draw bars
6759
barsPerCluster = len(bar_labels)
68-
barwidth = maxwidth / barsPerCluster
60+
if not overlap:
61+
barwidth = maxwidth / barsPerCluster
6962
bars = []
7063
for i in range(barsPerCluster):
7164
Ys = getBars(g['clusters'],i)
72-
bars.append(ax.bar(X+i*barwidth, Ys, width=barwidth, facecolor=cm.jet(1.0*i/len(bar_labels)), edgecolor="black"))
65+
if overlap:
66+
bars.append(ax.bar(X+0*barwidth, Ys, width=barwidth, facecolor=cm.jet(1.0*i/len(bar_labels)), edgecolor="black"))
67+
else:
68+
bars.append(ax.bar(X+i*barwidth, Ys, width=barwidth, facecolor=cm.jet(1.0*i/len(bar_labels)), edgecolor="black"))
7369
# now label values
7470
#for x,y in zip(X,Ys):
7571
# ax.text(x+barwidth*(0.5+i), y, y, ha="center", va="bottom")
@@ -89,24 +85,36 @@ def graph(g):
8985
ylim([0, 1.1*max([max(cluster) for cluster in clusters])])
9086

9187
#legend
92-
lgd = ax.legend(bars, bar_labels, bbox_to_anchor=(0.80, -0.3), ncol=2, fancybox=True)
88+
if len(bar_labels) > 1:
89+
lgd = ax.legend(bars, bar_labels, bbox_to_anchor=(0.80, -0.3), ncol=2, fancybox=True)
9390

9491
# fix spacing issues
9592
fig.tight_layout()
9693

97-
savefig(title+".pdf", dpi=80, bbox_extra_artists=(lgd,), bbox_inches='tight')
94+
if num_graphs > 1:
95+
save_name = "%s_%s" % (str(graph_num),out_file_base)
96+
else:
97+
save_name = out_file_base
9898

99-
def main():
99+
if len(bar_labels) > 1:
100+
savefig(save_name, dpi=80, bbox_extra_artists=(lgd,), bbox_inches='tight')
101+
else:
102+
savefig(save_name, dpi=80, bbox_inches='tight')
103+
104+
if __name__ == '__main__':
100105
# Parse arguments
101-
args = parse_args()
102-
input_file = args['input']
106+
if len(argv) < 3:
107+
print("Usage: ./generate-graph.py <.yaml file> <output filename base>")
108+
exit(1)
109+
110+
input_file_name = argv[1]
111+
out_file_base = argv[2]
112+
113+
input_file = open(input_file_name, 'r')
103114

104115
# parse yaml to find out what graphs we need to make
105116
graphs = parse_yaml(input_file)
106117

107118
# generate each graph
108-
for g in graphs:
109-
graph(g)
110-
111-
if __name__ == '__main__':
112-
main()
119+
for i in range(len(graphs)):
120+
graph(graphs[i], out_file_base, i, len(graphs))

0 commit comments

Comments
 (0)