Repository navigation
Expand file tree
/
Copy pathmatplotlib_cs.py
More file actions
125 lines (97 loc) · 3.02 KB
/
Copy pathmatplotlib_cs.py
File metadata and controls
125 lines (97 loc) · 3.02 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# -*- coding: utf-8 -*-
import json
import sys, getopt
import matplotlib.pyplot as plot
import matplotlib.patches as patches
import numpy as np
from matplotlib import rc
# Script builds a matplotlib figure based on information, passed to it through json file.
# Path to the JSON must be passed in first command line argument.
# a trick to enable text labels in cyrillic
rc('font', **{'sans-serif': 'Arial','family': 'sans-serif'})
def main(args):
if len(args) == 0:
print("No input path to json passed")
input("Press Enter to continue...")
return
print(args[0])
f = open(args[0], 'r')
json_raw = f.read()
task = json.loads(json_raw)
fig = plot.figure()
# precreate, make basic settings and save subplots
subplots = {}
subplot_index = 1
for i in range(0, task["rows"]):
for j in range(0, task["columns"]):
axes = fig.add_subplot(task["rows"], task["columns"], subplot_index)
subplots[subplot_index] = axes
set_grid(axes)
subplot_index += 1
# draw items on each subplot
for subplot in task["subplots"]:
for item in subplot["items"]:
axes = subplots[subplot["index"]]
plot.sca(axes)
set_titles(subplot)
if item["type"] == "Line2D":
plot_line2d(item)
elif item["type"] == "Histogram":
plot_histogram(item)
plot.tight_layout()
save_figure_to_file(task)
if not task["onlySaveImage"]:
plot.show()
def save_figure_to_file(task):
"""
Saves figure content to the file if it's path is provided
:type task: dict
:param task: Deserialized json with task description
:return:
"""
if "filename" in task and task["filename"] is not None:
print ("Saving figure to file {0}".format(task["filename"]))
plot.savefig(task["filename"], dpi=300)
def plot_line2d(line):
"""
Plots simple 2D line
:param line: Line description dict
:return:
"""
c = 'r' if "color" not in line else line["color"]
m = '' if "marker" not in line else line["marker"]
lw = 1 if "lineWidth" not in line else line["lineWidth"]
ms = 1 if "markerSize" not in line else line["markerSize"]
ls = '-' if "lineStyle" not in line else line["lineStyle"]
plot.plot(line["x"], line["y"], color=c, marker=m, lw=lw, ms=ms, ls=ls)
plot.hold(True)
def plot_histogram(hist):
"""
Plots simple 2D histogram
:param hist:
:return:
"""
plot.hist(hist["y"], 50)
plot.hold(True)
def set_grid(axes):
"""
Setup axes grid
:param axes:
:return:
"""
axes.grid(which='both')
axes.grid(which='minor', alpha=0.2)
axes.grid(which='major', alpha=0.5)
axes.grid('on')
def set_titles(task):
"""
Setup subplot X and Y axis titles
:param task:
:return:
"""
plot.title(u"{0}".format(task["title"]))
plot.ylabel(task["xtitle"])
plot.xlabel(task["ytitle"])
# entry point
if __name__ == "__main__":
main(sys.argv[1:])