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

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

""" 

The classes here provide support for using custom classes with 

Matplotlib, e.g., those that do not expose the array interface but know 

how to convert themselves to arrays. It also supports classes with 

units and units conversion. Use cases include converters for custom 

objects, e.g., a list of datetime objects, as well as for objects that 

are unit aware. We don't assume any particular units implementation; 

rather a units implementation must provide the register with the Registry 

converter dictionary and a `ConversionInterface`. For example, 

here is a complete implementation which supports plotting with native 

datetime objects:: 

 

import matplotlib.units as units 

import matplotlib.dates as dates 

import matplotlib.ticker as ticker 

import datetime 

 

class DateConverter(units.ConversionInterface): 

 

@staticmethod 

def convert(value, unit, axis): 

'Convert a datetime value to a scalar or array' 

return dates.date2num(value) 

 

@staticmethod 

def axisinfo(unit, axis): 

'Return major and minor tick locators and formatters' 

if unit!='date': return None 

majloc = dates.AutoDateLocator() 

majfmt = dates.AutoDateFormatter(majloc) 

return AxisInfo(majloc=majloc, 

majfmt=majfmt, 

label='date') 

 

@staticmethod 

def default_units(x, axis): 

'Return the default unit for x or None' 

return 'date' 

 

# Finally we register our object type with the Matplotlib units registry. 

units.registry[datetime.date] = DateConverter() 

 

""" 

 

from numbers import Number 

 

import numpy as np 

 

from matplotlib.cbook import iterable, safe_first_element 

 

 

class AxisInfo(object): 

""" 

Information to support default axis labeling, tick labeling, and 

default limits. An instance of this class must be returned by 

:meth:`ConversionInterface.axisinfo`. 

""" 

def __init__(self, majloc=None, minloc=None, 

majfmt=None, minfmt=None, label=None, 

default_limits=None): 

""" 

Parameters 

---------- 

majloc, minloc : Locator, optional 

Tick locators for the major and minor ticks. 

majfmt, minfmt : Formatter, optional 

Tick formatters for the major and minor ticks. 

label : str, optional 

The default axis label. 

default_limits : optional 

The default min and max limits of the axis if no data has 

been plotted. 

 

Notes 

----- 

If any of the above are ``None``, the axis will simply use the 

default value. 

""" 

self.majloc = majloc 

self.minloc = minloc 

self.majfmt = majfmt 

self.minfmt = minfmt 

self.label = label 

self.default_limits = default_limits 

 

 

class ConversionInterface(object): 

""" 

The minimal interface for a converter to take custom data types (or 

sequences) and convert them to values Matplotlib can use. 

""" 

@staticmethod 

def axisinfo(unit, axis): 

""" 

Return an `~units.AxisInfo` instance for the axis with the 

specified units. 

""" 

return None 

 

@staticmethod 

def default_units(x, axis): 

""" 

Return the default unit for *x* or ``None`` for the given axis. 

""" 

return None 

 

@staticmethod 

def convert(obj, unit, axis): 

""" 

Convert *obj* using *unit* for the specified *axis*. 

If *obj* is a sequence, return the converted sequence. 

The output must be a sequence of scalars that can be used by the numpy 

array layer. 

""" 

return obj 

 

@staticmethod 

def is_numlike(x): 

""" 

The Matplotlib datalim, autoscaling, locators etc work with 

scalars which are the units converted to floats given the 

current unit. The converter may be passed these floats, or 

arrays of them, even when units are set. 

""" 

if iterable(x): 

for thisx in x: 

return isinstance(thisx, Number) 

else: 

return isinstance(x, Number) 

 

 

class Registry(dict): 

""" 

A register that maps types to conversion interfaces. 

""" 

def __init__(self): 

dict.__init__(self) 

self._cached = {} 

 

def get_converter(self, x): 

""" 

Get the converter for data that has the same type as *x*. If no 

converters are registered for *x*, returns ``None``. 

""" 

 

if not len(self): 

return None # nothing registered 

# DISABLED idx = id(x) 

# DISABLED cached = self._cached.get(idx) 

# DISABLED if cached is not None: return cached 

 

converter = None 

classx = getattr(x, '__class__', None) 

 

if classx is not None: 

converter = self.get(classx) 

 

if converter is None and hasattr(x, "values"): 

# this unpacks pandas series or dataframes... 

x = x.values 

 

# If x is an array, look inside the array for data with units 

if isinstance(x, np.ndarray) and x.size: 

xravel = x.ravel() 

try: 

# pass the first value of x that is not masked back to 

# get_converter 

if not np.all(xravel.mask): 

# some elements are not masked 

converter = self.get_converter( 

xravel[np.argmin(xravel.mask)]) 

return converter 

except AttributeError: 

# not a masked_array 

# Make sure we don't recurse forever -- it's possible for 

# ndarray subclasses to continue to return subclasses and 

# not ever return a non-subclass for a single element. 

next_item = xravel[0] 

if (not isinstance(next_item, np.ndarray) or 

next_item.shape != x.shape): 

converter = self.get_converter(next_item) 

return converter 

 

# If we haven't found a converter yet, try to get the first element 

if converter is None: 

try: 

thisx = safe_first_element(x) 

except (TypeError, StopIteration): 

pass 

else: 

if classx and classx != getattr(thisx, '__class__', None): 

converter = self.get_converter(thisx) 

return converter 

 

# DISABLED self._cached[idx] = converter 

return converter 

 

 

registry = Registry()