def _parse_char_metrics(fh): """ Return a character metric dictionary. Keys are the ASCII num of the character, values are a (*wx*, *name*, *bbox*) tuple, where *wx* is the character width, *name* is the postscript language name, and *bbox* is a (*llx*, *lly*, *urx*, *ury*) tuple. This function is incomplete per the standard, but thus far parses all the sample afm files tried. """ ascii_d = {} name_d = {} while 1: line = fh.readline() if not line: break line = line.rstrip().decode('ascii') # Convert from byte-literal if line.startswith('EndCharMetrics'): return ascii_d, name_d # Split the metric line into a dictonary, keyed by metric identifiers vals = filter(lambda s: len(s) > 0, line.split(';')) vals = dict(map(lambda s: tuple(s.strip().split(' ', 1)), vals)) # There may be other metrics present, but only these are needed if any([id not in vals.keys() for id in ('C', 'WX', 'N', 'B')]): raise RuntimeError('Bad char metrics line: %s' % line) num = _to_int(vals['C']) wx = _to_float(vals['WX']) name = vals['N'] bbox = _to_list_of_floats(vals['B']) bbox = list(map(int, bbox)) # Workaround: If the character name is 'Euro', give it the # corresponding character code, according to WinAnsiEncoding (see PDF # Reference). if name == 'Euro': num = 128 if num != -1: ascii_d[num] = (wx, name, bbox) name_d[name] = (wx, bbox) raise RuntimeError('Bad parse')
def __init__(self, fig, *args, **kwargs): """ *fig* is a :class:`matplotlib.figure.Figure` instance. *args* is the tuple (*numRows*, *numCols*, *plotNum*), where the array of subplots in the figure has dimensions *numRows*, *numCols*, and where *plotNum* is the number of the subplot being created. *plotNum* starts at 1 in the upper left corner and increases to the right. If *numRows* <= *numCols* <= *plotNum* < 10, *args* can be the decimal integer *numRows* * 100 + *numCols* * 10 + *plotNum*. """ self.figure = fig if len(args) == 1: if isinstance(args[0], SubplotSpec): self._subplotspec = args[0] else: try: s = str(int(args[0])) rows, cols, num = list(map(int, s)) except ValueError: raise ValueError( 'Single argument to subplot must be a 3-digit ' 'integer') self._subplotspec = GridSpec(rows, cols)[num - 1] # num - 1 for converting from MATLAB to python indexing elif len(args) == 3: rows, cols, num = args rows = int(rows) cols = int(cols) if isinstance(num, tuple) and len(num) == 2: num = [int(n) for n in num] self._subplotspec = GridSpec(rows, cols)[num[0] - 1:num[1]] else: if num < 1 or num > rows*cols: raise ValueError( "num must be 1 <= num <= {maxn}, not {num}".format( maxn=rows*cols, num=num)) self._subplotspec = GridSpec(rows, cols)[int(num) - 1] # num - 1 for converting from MATLAB to python indexing else: raise ValueError('Illegal argument(s) to subplot: %s' % (args,)) self.update_params() # _axes_class is set in the subplot_class_factory self._axes_class.__init__(self, fig, self.figbox, **kwargs)
def __init__(self, fig, *args, **kwargs): """ *fig* is a :class:`matplotlib.figure.Figure` instance. *args* is the tuple (*numRows*, *numCols*, *plotNum*), where the array of subplots in the figure has dimensions *numRows*, *numCols*, and where *plotNum* is the number of the subplot being created. *plotNum* starts at 1 in the upper left corner and increases to the right. If *numRows* <= *numCols* <= *plotNum* < 10, *args* can be the decimal integer *numRows* * 100 + *numCols* * 10 + *plotNum*. """ self.figure = fig if len(args) == 1: if isinstance(args[0], SubplotSpec): self._subplotspec = args[0] else: try: s = str(int(args[0])) rows, cols, num = list(map(int, s)) except ValueError: raise ValueError( 'Single argument to subplot must be a 3-digit integer') self._subplotspec = GridSpec(rows, cols)[num - 1] # num - 1 for converting from MATLAB to python indexing elif len(args) == 3: rows, cols, num = args rows = int(rows) cols = int(cols) if isinstance(num, tuple) and len(num) == 2: num = [int(n) for n in num] self._subplotspec = GridSpec(rows, cols)[num[0] - 1:num[1]] else: self._subplotspec = GridSpec(rows, cols)[int(num) - 1] # num - 1 for converting from MATLAB to python indexing else: raise ValueError('Illegal argument(s) to subplot: %s' % (args, )) # total = rows*cols # num -= 1 # convert from matlab to python indexing # # i.e., num in range(0,total) # if num >= total: # raise ValueError( 'Subplot number exceeds total subplots') # self._rows = rows # self._cols = cols # self._num = num # self.update_params() # sets self.fixbox self.update_params() pos = self.figbox.bounds horizontal = kwargs.pop("horizontal", []) vertical = kwargs.pop("vertical", []) aspect = kwargs.pop("aspect", None) anchor = kwargs.pop("anchor", "C") if kwargs: raise Exception("") Divider.__init__(self, fig, pos, horizontal, vertical, aspect=aspect, anchor=anchor)
def __init__(self, fig, *args, **kwargs): """ Parameters ---------- fig : :class:`matplotlib.figure.Figure` args : tuple (*numRows*, *numCols*, *plotNum*) The array of subplots in the figure has dimensions *numRows*, *numCols*, and *plotNum* is the number of the subplot being created. *plotNum* starts at 1 in the upper left corner and increases to the right. If *numRows* <= *numCols* <= *plotNum* < 10, *args* can be the decimal integer *numRows* * 100 + *numCols* * 10 + *plotNum*. """ self.figure = fig if len(args) == 1: if isinstance(args[0], SubplotSpec): self._subplotspec = args[0] else: try: s = str(int(args[0])) rows, cols, num = list(map(int, s)) except ValueError: raise ValueError( 'Single argument to subplot must be a 3-digit integer') self._subplotspec = GridSpec(rows, cols)[num-1] # num - 1 for converting from MATLAB to python indexing elif len(args) == 3: rows, cols, num = args rows = int(rows) cols = int(cols) if isinstance(num, tuple) and len(num) == 2: num = [int(n) for n in num] self._subplotspec = GridSpec(rows, cols)[num[0]-1:num[1]] else: self._subplotspec = GridSpec(rows, cols)[int(num)-1] # num - 1 for converting from MATLAB to python indexing else: raise ValueError('Illegal argument(s) to subplot: %s' % (args,)) # total = rows*cols # num -= 1 # convert from matlab to python indexing # # i.e., num in range(0,total) # if num >= total: # raise ValueError( 'Subplot number exceeds total subplots') # self._rows = rows # self._cols = cols # self._num = num # self.update_params() # sets self.fixbox self.update_params() pos = self.figbox.bounds horizontal = kwargs.pop("horizontal", []) vertical = kwargs.pop("vertical", []) aspect = kwargs.pop("aspect", None) anchor = kwargs.pop("anchor", "C") if kwargs: raise Exception("") Divider.__init__(self, fig, pos, horizontal, vertical, aspect=aspect, anchor=anchor)
def test_auto_date_locator(): def _create_auto_date_locator(date1, date2): locator = mdates.AutoDateLocator() locator.create_dummy_axis() locator.set_view_interval(mdates.date2num(date1), mdates.date2num(date2)) return locator d1 = datetime.datetime(1990, 1, 1) results = ( [ datetime.timedelta(weeks=52 * 200), [ '1990-01-01 00:00:00+00:00', '2010-01-01 00:00:00+00:00', '2030-01-01 00:00:00+00:00', '2050-01-01 00:00:00+00:00', '2070-01-01 00:00:00+00:00', '2090-01-01 00:00:00+00:00', '2110-01-01 00:00:00+00:00', '2130-01-01 00:00:00+00:00', '2150-01-01 00:00:00+00:00', '2170-01-01 00:00:00+00:00' ] ], [ datetime.timedelta(weeks=52), [ '1990-01-01 00:00:00+00:00', '1990-02-01 00:00:00+00:00', '1990-03-01 00:00:00+00:00', '1990-04-01 00:00:00+00:00', '1990-05-01 00:00:00+00:00', '1990-06-01 00:00:00+00:00', '1990-07-01 00:00:00+00:00', '1990-08-01 00:00:00+00:00', '1990-09-01 00:00:00+00:00', '1990-10-01 00:00:00+00:00', '1990-11-01 00:00:00+00:00', '1990-12-01 00:00:00+00:00' ] ], [ datetime.timedelta(days=141), [ '1990-01-05 00:00:00+00:00', '1990-01-26 00:00:00+00:00', '1990-02-16 00:00:00+00:00', '1990-03-09 00:00:00+00:00', '1990-03-30 00:00:00+00:00', '1990-04-20 00:00:00+00:00', '1990-05-11 00:00:00+00:00' ] ], [ datetime.timedelta(days=40), [ '1990-01-03 00:00:00+00:00', '1990-01-10 00:00:00+00:00', '1990-01-17 00:00:00+00:00', '1990-01-24 00:00:00+00:00', '1990-01-31 00:00:00+00:00', '1990-02-07 00:00:00+00:00' ] ], [ datetime.timedelta(hours=40), [ '1990-01-01 00:00:00+00:00', '1990-01-01 04:00:00+00:00', '1990-01-01 08:00:00+00:00', '1990-01-01 12:00:00+00:00', '1990-01-01 16:00:00+00:00', '1990-01-01 20:00:00+00:00', '1990-01-02 00:00:00+00:00', '1990-01-02 04:00:00+00:00', '1990-01-02 08:00:00+00:00', '1990-01-02 12:00:00+00:00', '1990-01-02 16:00:00+00:00' ] ], [ datetime.timedelta(minutes=20), [ '1990-01-01 00:00:00+00:00', '1990-01-01 00:05:00+00:00', '1990-01-01 00:10:00+00:00', '1990-01-01 00:15:00+00:00', '1990-01-01 00:20:00+00:00' ] ], [ datetime.timedelta(seconds=40), [ '1990-01-01 00:00:00+00:00', '1990-01-01 00:00:05+00:00', '1990-01-01 00:00:10+00:00', '1990-01-01 00:00:15+00:00', '1990-01-01 00:00:20+00:00', '1990-01-01 00:00:25+00:00', '1990-01-01 00:00:30+00:00', '1990-01-01 00:00:35+00:00', '1990-01-01 00:00:40+00:00' ] ], [ datetime.timedelta(microseconds=1500), [ '1989-12-31 23:59:59.999507+00:00', '1990-01-01 00:00:00+00:00', '1990-01-01 00:00:00.000502+00:00', '1990-01-01 00:00:00.001005+00:00', '1990-01-01 00:00:00.001508+00:00' ] ], ) for t_delta, expected in results: d2 = d1 + t_delta locator = _create_auto_date_locator(d1, d2) assert_equal(list(map(str, mdates.num2date(locator()))), expected)
def test_auto_date_locator(): def _create_auto_date_locator(date1, date2): locator = mdates.AutoDateLocator() locator.create_dummy_axis() locator.set_view_interval(mdates.date2num(date1), mdates.date2num(date2)) return locator d1 = datetime.datetime(1990, 1, 1) results = ([datetime.timedelta(weeks=52 * 200), ['1990-01-01 00:00:00+00:00', '2010-01-01 00:00:00+00:00', '2030-01-01 00:00:00+00:00', '2050-01-01 00:00:00+00:00', '2070-01-01 00:00:00+00:00', '2090-01-01 00:00:00+00:00', '2110-01-01 00:00:00+00:00', '2130-01-01 00:00:00+00:00', '2150-01-01 00:00:00+00:00', '2170-01-01 00:00:00+00:00'] ], [datetime.timedelta(weeks=52), ['1990-01-01 00:00:00+00:00', '1990-02-01 00:00:00+00:00', '1990-03-01 00:00:00+00:00', '1990-04-01 00:00:00+00:00', '1990-05-01 00:00:00+00:00', '1990-06-01 00:00:00+00:00', '1990-07-01 00:00:00+00:00', '1990-08-01 00:00:00+00:00', '1990-09-01 00:00:00+00:00', '1990-10-01 00:00:00+00:00', '1990-11-01 00:00:00+00:00', '1990-12-01 00:00:00+00:00'] ], [datetime.timedelta(days=141), ['1990-01-05 00:00:00+00:00', '1990-01-26 00:00:00+00:00', '1990-02-16 00:00:00+00:00', '1990-03-09 00:00:00+00:00', '1990-03-30 00:00:00+00:00', '1990-04-20 00:00:00+00:00', '1990-05-11 00:00:00+00:00'] ], [datetime.timedelta(days=40), ['1990-01-03 00:00:00+00:00', '1990-01-10 00:00:00+00:00', '1990-01-17 00:00:00+00:00', '1990-01-24 00:00:00+00:00', '1990-01-31 00:00:00+00:00', '1990-02-07 00:00:00+00:00'] ], [datetime.timedelta(hours=40), ['1990-01-01 00:00:00+00:00', '1990-01-01 04:00:00+00:00', '1990-01-01 08:00:00+00:00', '1990-01-01 12:00:00+00:00', '1990-01-01 16:00:00+00:00', '1990-01-01 20:00:00+00:00', '1990-01-02 00:00:00+00:00', '1990-01-02 04:00:00+00:00', '1990-01-02 08:00:00+00:00', '1990-01-02 12:00:00+00:00', '1990-01-02 16:00:00+00:00'] ], [datetime.timedelta(minutes=20), ['1990-01-01 00:00:00+00:00', '1990-01-01 00:05:00+00:00', '1990-01-01 00:10:00+00:00', '1990-01-01 00:15:00+00:00', '1990-01-01 00:20:00+00:00'] ], [datetime.timedelta(seconds=40), ['1990-01-01 00:00:00+00:00', '1990-01-01 00:00:05+00:00', '1990-01-01 00:00:10+00:00', '1990-01-01 00:00:15+00:00', '1990-01-01 00:00:20+00:00', '1990-01-01 00:00:25+00:00', '1990-01-01 00:00:30+00:00', '1990-01-01 00:00:35+00:00', '1990-01-01 00:00:40+00:00'] ], [datetime.timedelta(microseconds=1500), ['1989-12-31 23:59:59.999507+00:00', '1990-01-01 00:00:00+00:00', '1990-01-01 00:00:00.000502+00:00', '1990-01-01 00:00:00.001005+00:00', '1990-01-01 00:00:00.001508+00:00'] ], ) for t_delta, expected in results: d2 = d1 + t_delta locator = _create_auto_date_locator(d1, d2) assert_equal(list(map(str, mdates.num2date(locator()))), expected)