1+ import matplotlib .pyplot as plt
2+ import json
3+
4+ # Note, this code is a modified version from these pages:
5+ #
6+ # http://www.randalolson.com/2014/06/28/how-to-make-beautiful-data-visualizations-in-python-with-matplotlib/
7+ # http://matplotlib.org/examples/pylab_examples/subplots_demo.html
8+
9+ # These are the "Tableau 20" colors as RGB.
10+ tableau20 = [(31 , 119 , 180 ), (174 , 199 , 232 ), (255 , 127 , 14 ), (255 , 187 , 120 ),
11+ (44 , 160 , 44 ), (152 , 223 , 138 ), (214 , 39 , 40 ), (255 , 152 , 150 ),
12+ (148 , 103 , 189 ), (197 , 176 , 213 ), (140 , 86 , 75 ), (196 , 156 , 148 ),
13+ (227 , 119 , 194 ), (247 , 182 , 210 ), (127 , 127 , 127 ), (199 , 199 , 199 ),
14+ (188 , 189 , 34 ), (219 , 219 , 141 ), (23 , 190 , 207 ), (158 , 218 , 229 )]
15+
16+ # Scale the RGB values to the [0, 1] range, which is the format matplotlib accepts.
17+ for i in range (len (tableau20 )):
18+ r , g , b = tableau20 [i ]
19+ tableau20 [i ] = (r / 255. , g / 255. , b / 255. )
20+
21+ plt .style .use (['dark_background' ])
22+
23+ test = json .loads ( open ( 'motorlog-fast-ramp.json' ).read () )
24+
25+ values = {}
26+
27+ # Extract the data from the log in a format that's useful for plotting
28+
29+ for k ,d in test ['data' ].items ():
30+ values ['k' ] = {}
31+ values ['k' ]['x' ] = [row [0 ] for row in d ]
32+ values ['k' ]['y' ] = []
33+
34+ for i ,a in enumerate (test ['meta' ]['ports' ][k ]['log_attributes' ]):
35+ values ['k' ]['y' ].append ( {'name' : a , 'values' : [row [1 ][i ] for row in d ]})
36+
37+ # You typically want your plot to be ~1.33x wider than tall. This plot is a rare
38+ # exception because of the number of lines being plotted on it.
39+ # Common sizes: (10, 7.5) and (12, 9)
40+ # plt.figure(figsize=(12, 9))
41+
42+ f , axarr = plt .subplots (3 , sharex = True )
43+
44+ axarr [2 ].set_xlabel ('Time (seconds)' )
45+
46+ # Clean up the chartjunk
47+ for i ,ax in enumerate (axarr ):
48+ print i , ax
49+ # Remove the plot frame lines. They are unnecessary chartjunk.
50+ ax .spines ["top" ].set_visible (False )
51+ # ax.spines["bottom"].set_visible(False)
52+ ax .spines ["right" ].set_visible (False )
53+ # ax.spines["left"].set_visible(False)
54+
55+ # Ensure that the axis ticks only show up on the bottom and left of the plot.
56+ # Ticks on the right and top of the plot are generally unnecessary chartjunk.
57+ ax .get_xaxis ().tick_bottom ()
58+ ax .get_yaxis ().tick_left ()
59+
60+ # # Limit the range of the plot to only where the data is.
61+ # # Avoid unnecessary whitespace.
62+ # plt.ylim(0, 90)
63+ # plt.xlim(1968, 2014)
64+ #
65+ # # Make sure your axis ticks are large enough to be easily read.
66+ # # You don't want your viewers squinting to read your plot.
67+ # plt.yticks(range(0, 91, 10), [str(x) + "%" for x in range(0, 91, 10)], fontsize=14)
68+ # plt.xticks(fontsize=14)
69+
70+ axarr [i ].plot (values ['k' ]['x' ],values ['k' ]['y' ][i ]['values' ], lw = 2.5 , color = tableau20 [i ] )
71+ axarr [i ].text (.8 ,1 , "{0}:{1}" .format ( k , values ['k' ]['y' ][i ]['name' ] ),
72+ fontsize = 14 ,
73+ color = tableau20 [i ],
74+ horizontalalignment = 'left' ,
75+ verticalalignment = 'center' ,
76+ transform = axarr [i ].transAxes )
77+
78+ #axarr[0].plot(x, y0, lw=2.5, color=tableau20[0])
79+ # y_pos = y0[-1]
80+ #plt.text(4000, y_pos, 'speed', fontsize=14, color=tableau20[0])
81+
82+ #axarr[1].plot(x, y1, lw=2.5, color=tableau20[1])
83+ # y_pos = y1[-1]
84+ #plt.text(4000, y_pos, 'position', fontsize=14, color=tableau20[1])
85+
86+ #axarr[2].plot(x, y2, lw=2.5, color=tableau20[2])
87+ # y_pos = y0[-1]
88+ #plt.text(4000, y_pos, 'duty_cycle', fontsize=14, color=tableau20[2])
89+
90+ # Note that if the title is descriptive enough, it is unnecessary to include
91+ # axis labels; they are self-evident, in this plot's case.
92+
93+ #plt.text(1995, 93, "Percentage of Bachelor's degrees conferred to women in the U.S.A."
94+ # ", by major (1970-2012)", fontsize=17, ha="center")
95+
96+ # Always include your data source(s) and copyright notice! And for your
97+ # data sources, tell your viewers exactly where the data came from,
98+ # preferably with a direct link to the data. Just telling your viewers
99+ # that you used data from the "U.S. Census Bureau" is completely useless:
100+ # the U.S. Census Bureau provides all kinds of data, so how are your
101+ # viewers supposed to know which data set you used?
102+
103+ #plt.text(1966, -8, "Data source: nces.ed.gov/programs/digest/2013menu_tables.asp"
104+ # "\nAuthor: Randy Olson (randalolson.com / @randal_olson)"
105+ # "\nNote: Some majors are missing because the historical data "
106+ # "is not available for them", fontsize=10)
107+
108+ # Finally, save the figure as a PNG.
109+ # You can also save it as a PDF, JPEG, etc.
110+ # Just change the file extension in this call.
111+ # bbox_inches="tight" removes all the extra whitespace on the edges of your plot.
112+
113+ plt .savefig ("motorlog-fast-ramp-{0}.png" .format (k ), bbox_inches = "tight" )
0 commit comments