StrConverter.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. """StrConverter module containing class StrConverter."""
  2. import numpy as np
  3. import matplotlib.units as units
  4. __all__ = ['StrConverter']
  5. class StrConverter(units.ConversionInterface):
  6. """: A matplotlib converter class. Provides matplotlib conversion
  7. functionality for string data values.
  8. Valid units for string are:
  9. - 'indexed' : Values are indexed as they are specified for plotting.
  10. - 'sorted' : Values are sorted alphanumerically.
  11. - 'inverted' : Values are inverted so that the first value is on top.
  12. - 'sorted-inverted' : A combination of 'sorted' and 'inverted'
  13. """
  14. @staticmethod
  15. def axisinfo(unit, axis):
  16. """: Returns information on how to handle an axis that has string data.
  17. = INPUT VARIABLES
  18. - axis The axis using this converter.
  19. - unit The units to use for a axis with string data.
  20. = RETURN VALUE
  21. - Returns a matplotlib AxisInfo data structure that contains
  22. minor/major formatters, major/minor locators, and default
  23. label information.
  24. """
  25. return None
  26. @staticmethod
  27. def convert(value, unit, axis):
  28. """: Convert value using unit to a float. If value is a sequence, return
  29. the converted sequence.
  30. = INPUT VARIABLES
  31. - axis The axis using this converter.
  32. - value The value or list of values that need to be converted.
  33. - unit The units to use for a axis with Epoch data.
  34. = RETURN VALUE
  35. - Returns the value parameter converted to floats.
  36. """
  37. if units.ConversionInterface.is_numlike(value):
  38. return value
  39. if value == []:
  40. return []
  41. # we delay loading to make matplotlib happy
  42. ax = axis.axes
  43. if axis is ax.get_xaxis():
  44. isXAxis = True
  45. else:
  46. isXAxis = False
  47. axis.get_major_ticks()
  48. ticks = axis.get_ticklocs()
  49. labels = axis.get_ticklabels()
  50. labels = [l.get_text() for l in labels if l.get_text()]
  51. if not labels:
  52. ticks = []
  53. labels = []
  54. if not np.iterable(value):
  55. value = [value]
  56. newValues = []
  57. for v in value:
  58. if v not in labels and v not in newValues:
  59. newValues.append(v)
  60. labels.extend(newValues)
  61. # DISABLED: This is disabled because matplotlib bar plots do not
  62. # DISABLED: recalculate the unit conversion of the data values
  63. # DISABLED: this is due to design and is not really a bug.
  64. # DISABLED: If this gets changed, then we can activate the following
  65. # DISABLED: block of code. Note that this works for line plots.
  66. # DISABLED if unit:
  67. # DISABLED if unit.find("sorted") > -1:
  68. # DISABLED labels.sort()
  69. # DISABLED if unit.find("inverted") > -1:
  70. # DISABLED labels = labels[::-1]
  71. # add padding (so they do not appear on the axes themselves)
  72. labels = [''] + labels + ['']
  73. ticks = list(range(len(labels)))
  74. ticks[0] = 0.5
  75. ticks[-1] = ticks[-1] - 0.5
  76. axis.set_ticks(ticks)
  77. axis.set_ticklabels(labels)
  78. # we have to do the following lines to make ax.autoscale_view work
  79. loc = axis.get_major_locator()
  80. loc.set_bounds(ticks[0], ticks[-1])
  81. if isXAxis:
  82. ax.set_xlim(ticks[0], ticks[-1])
  83. else:
  84. ax.set_ylim(ticks[0], ticks[-1])
  85. result = [ticks[labels.index(v)] for v in value]
  86. ax.viewLim.ignore(-1)
  87. return result
  88. @staticmethod
  89. def default_units(value, axis):
  90. """: Return the default unit for value, or None.
  91. = INPUT VARIABLES
  92. - axis The axis using this converter.
  93. - value The value or list of values that need units.
  94. = RETURN VALUE
  95. - Returns the default units to use for value.
  96. Return the default unit for value, or None.
  97. """
  98. # The default behavior for string indexing.
  99. return "indexed"