tools.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. # being a bit too dynamic
  2. from math import ceil
  3. import warnings
  4. import matplotlib.table
  5. import matplotlib.ticker as ticker
  6. import numpy as np
  7. from pandas.core.dtypes.common import is_list_like
  8. from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries
  9. from pandas.plotting._matplotlib import compat
  10. def format_date_labels(ax, rot):
  11. # mini version of autofmt_xdate
  12. for label in ax.get_xticklabels():
  13. label.set_ha("right")
  14. label.set_rotation(rot)
  15. fig = ax.get_figure()
  16. fig.subplots_adjust(bottom=0.2)
  17. def table(ax, data, rowLabels=None, colLabels=None, **kwargs):
  18. if isinstance(data, ABCSeries):
  19. data = data.to_frame()
  20. elif isinstance(data, ABCDataFrame):
  21. pass
  22. else:
  23. raise ValueError("Input data must be DataFrame or Series")
  24. if rowLabels is None:
  25. rowLabels = data.index
  26. if colLabels is None:
  27. colLabels = data.columns
  28. cellText = data.values
  29. table = matplotlib.table.table(
  30. ax, cellText=cellText, rowLabels=rowLabels, colLabels=colLabels, **kwargs
  31. )
  32. return table
  33. def _get_layout(nplots, layout=None, layout_type="box"):
  34. if layout is not None:
  35. if not isinstance(layout, (tuple, list)) or len(layout) != 2:
  36. raise ValueError("Layout must be a tuple of (rows, columns)")
  37. nrows, ncols = layout
  38. # Python 2 compat
  39. ceil_ = lambda x: int(ceil(x))
  40. if nrows == -1 and ncols > 0:
  41. layout = nrows, ncols = (ceil_(float(nplots) / ncols), ncols)
  42. elif ncols == -1 and nrows > 0:
  43. layout = nrows, ncols = (nrows, ceil_(float(nplots) / nrows))
  44. elif ncols <= 0 and nrows <= 0:
  45. msg = "At least one dimension of layout must be positive"
  46. raise ValueError(msg)
  47. if nrows * ncols < nplots:
  48. raise ValueError(
  49. f"Layout of {nrows}x{ncols} must be larger than required size {nplots}"
  50. )
  51. return layout
  52. if layout_type == "single":
  53. return (1, 1)
  54. elif layout_type == "horizontal":
  55. return (1, nplots)
  56. elif layout_type == "vertical":
  57. return (nplots, 1)
  58. layouts = {1: (1, 1), 2: (1, 2), 3: (2, 2), 4: (2, 2)}
  59. try:
  60. return layouts[nplots]
  61. except KeyError:
  62. k = 1
  63. while k ** 2 < nplots:
  64. k += 1
  65. if (k - 1) * k >= nplots:
  66. return k, (k - 1)
  67. else:
  68. return k, k
  69. # copied from matplotlib/pyplot.py and modified for pandas.plotting
  70. def _subplots(
  71. naxes=None,
  72. sharex=False,
  73. sharey=False,
  74. squeeze=True,
  75. subplot_kw=None,
  76. ax=None,
  77. layout=None,
  78. layout_type="box",
  79. **fig_kw,
  80. ):
  81. """Create a figure with a set of subplots already made.
  82. This utility wrapper makes it convenient to create common layouts of
  83. subplots, including the enclosing figure object, in a single call.
  84. Keyword arguments:
  85. naxes : int
  86. Number of required axes. Exceeded axes are set invisible. Default is
  87. nrows * ncols.
  88. sharex : bool
  89. If True, the X axis will be shared amongst all subplots.
  90. sharey : bool
  91. If True, the Y axis will be shared amongst all subplots.
  92. squeeze : bool
  93. If True, extra dimensions are squeezed out from the returned axis object:
  94. - if only one subplot is constructed (nrows=ncols=1), the resulting
  95. single Axis object is returned as a scalar.
  96. - for Nx1 or 1xN subplots, the returned object is a 1-d numpy object
  97. array of Axis objects are returned as numpy 1-d arrays.
  98. - for NxM subplots with N>1 and M>1 are returned as a 2d array.
  99. If False, no squeezing is done: the returned axis object is always
  100. a 2-d array containing Axis instances, even if it ends up being 1x1.
  101. subplot_kw : dict
  102. Dict with keywords passed to the add_subplot() call used to create each
  103. subplots.
  104. ax : Matplotlib axis object, optional
  105. layout : tuple
  106. Number of rows and columns of the subplot grid.
  107. If not specified, calculated from naxes and layout_type
  108. layout_type : {'box', 'horizontal', 'vertical'}, default 'box'
  109. Specify how to layout the subplot grid.
  110. fig_kw : Other keyword arguments to be passed to the figure() call.
  111. Note that all keywords not recognized above will be
  112. automatically included here.
  113. Returns:
  114. fig, ax : tuple
  115. - fig is the Matplotlib Figure object
  116. - ax can be either a single axis object or an array of axis objects if
  117. more than one subplot was created. The dimensions of the resulting array
  118. can be controlled with the squeeze keyword, see above.
  119. **Examples:**
  120. x = np.linspace(0, 2*np.pi, 400)
  121. y = np.sin(x**2)
  122. # Just a figure and one subplot
  123. f, ax = plt.subplots()
  124. ax.plot(x, y)
  125. ax.set_title('Simple plot')
  126. # Two subplots, unpack the output array immediately
  127. f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
  128. ax1.plot(x, y)
  129. ax1.set_title('Sharing Y axis')
  130. ax2.scatter(x, y)
  131. # Four polar axes
  132. plt.subplots(2, 2, subplot_kw=dict(polar=True))
  133. """
  134. import matplotlib.pyplot as plt
  135. if subplot_kw is None:
  136. subplot_kw = {}
  137. if ax is None:
  138. fig = plt.figure(**fig_kw)
  139. else:
  140. if is_list_like(ax):
  141. ax = _flatten(ax)
  142. if layout is not None:
  143. warnings.warn(
  144. "When passing multiple axes, layout keyword is ignored", UserWarning
  145. )
  146. if sharex or sharey:
  147. warnings.warn(
  148. "When passing multiple axes, sharex and sharey "
  149. "are ignored. These settings must be specified "
  150. "when creating axes",
  151. UserWarning,
  152. stacklevel=4,
  153. )
  154. if len(ax) == naxes:
  155. fig = ax[0].get_figure()
  156. return fig, ax
  157. else:
  158. raise ValueError(
  159. f"The number of passed axes must be {naxes}, the "
  160. "same as the output plot"
  161. )
  162. fig = ax.get_figure()
  163. # if ax is passed and a number of subplots is 1, return ax as it is
  164. if naxes == 1:
  165. if squeeze:
  166. return fig, ax
  167. else:
  168. return fig, _flatten(ax)
  169. else:
  170. warnings.warn(
  171. "To output multiple subplots, the figure containing "
  172. "the passed axes is being cleared",
  173. UserWarning,
  174. stacklevel=4,
  175. )
  176. fig.clear()
  177. nrows, ncols = _get_layout(naxes, layout=layout, layout_type=layout_type)
  178. nplots = nrows * ncols
  179. # Create empty object array to hold all axes. It's easiest to make it 1-d
  180. # so we can just append subplots upon creation, and then
  181. axarr = np.empty(nplots, dtype=object)
  182. # Create first subplot separately, so we can share it if requested
  183. ax0 = fig.add_subplot(nrows, ncols, 1, **subplot_kw)
  184. if sharex:
  185. subplot_kw["sharex"] = ax0
  186. if sharey:
  187. subplot_kw["sharey"] = ax0
  188. axarr[0] = ax0
  189. # Note off-by-one counting because add_subplot uses the MATLAB 1-based
  190. # convention.
  191. for i in range(1, nplots):
  192. kwds = subplot_kw.copy()
  193. # Set sharex and sharey to None for blank/dummy axes, these can
  194. # interfere with proper axis limits on the visible axes if
  195. # they share axes e.g. issue #7528
  196. if i >= naxes:
  197. kwds["sharex"] = None
  198. kwds["sharey"] = None
  199. ax = fig.add_subplot(nrows, ncols, i + 1, **kwds)
  200. axarr[i] = ax
  201. if naxes != nplots:
  202. for ax in axarr[naxes:]:
  203. ax.set_visible(False)
  204. _handle_shared_axes(axarr, nplots, naxes, nrows, ncols, sharex, sharey)
  205. if squeeze:
  206. # Reshape the array to have the final desired dimension (nrow,ncol),
  207. # though discarding unneeded dimensions that equal 1. If we only have
  208. # one subplot, just return it instead of a 1-element array.
  209. if nplots == 1:
  210. axes = axarr[0]
  211. else:
  212. axes = axarr.reshape(nrows, ncols).squeeze()
  213. else:
  214. # returned axis array will be always 2-d, even if nrows=ncols=1
  215. axes = axarr.reshape(nrows, ncols)
  216. return fig, axes
  217. def _remove_labels_from_axis(axis):
  218. for t in axis.get_majorticklabels():
  219. t.set_visible(False)
  220. # set_visible will not be effective if
  221. # minor axis has NullLocator and NullFormattor (default)
  222. if isinstance(axis.get_minor_locator(), ticker.NullLocator):
  223. axis.set_minor_locator(ticker.AutoLocator())
  224. if isinstance(axis.get_minor_formatter(), ticker.NullFormatter):
  225. axis.set_minor_formatter(ticker.FormatStrFormatter(""))
  226. for t in axis.get_minorticklabels():
  227. t.set_visible(False)
  228. axis.get_label().set_visible(False)
  229. def _handle_shared_axes(axarr, nplots, naxes, nrows, ncols, sharex, sharey):
  230. if nplots > 1:
  231. if compat._mpl_ge_3_2_0():
  232. row_num = lambda x: x.get_subplotspec().rowspan.start
  233. col_num = lambda x: x.get_subplotspec().colspan.start
  234. else:
  235. row_num = lambda x: x.rowNum
  236. col_num = lambda x: x.colNum
  237. if nrows > 1:
  238. try:
  239. # first find out the ax layout,
  240. # so that we can correctly handle 'gaps"
  241. layout = np.zeros((nrows + 1, ncols + 1), dtype=np.bool)
  242. for ax in axarr:
  243. layout[row_num(ax), col_num(ax)] = ax.get_visible()
  244. for ax in axarr:
  245. # only the last row of subplots should get x labels -> all
  246. # other off layout handles the case that the subplot is
  247. # the last in the column, because below is no subplot/gap.
  248. if not layout[row_num(ax) + 1, col_num(ax)]:
  249. continue
  250. if sharex or len(ax.get_shared_x_axes().get_siblings(ax)) > 1:
  251. _remove_labels_from_axis(ax.xaxis)
  252. except IndexError:
  253. # if gridspec is used, ax.rowNum and ax.colNum may different
  254. # from layout shape. in this case, use last_row logic
  255. for ax in axarr:
  256. if ax.is_last_row():
  257. continue
  258. if sharex or len(ax.get_shared_x_axes().get_siblings(ax)) > 1:
  259. _remove_labels_from_axis(ax.xaxis)
  260. if ncols > 1:
  261. for ax in axarr:
  262. # only the first column should get y labels -> set all other to
  263. # off as we only have labels in the first column and we always
  264. # have a subplot there, we can skip the layout test
  265. if ax.is_first_col():
  266. continue
  267. if sharey or len(ax.get_shared_y_axes().get_siblings(ax)) > 1:
  268. _remove_labels_from_axis(ax.yaxis)
  269. def _flatten(axes):
  270. if not is_list_like(axes):
  271. return np.array([axes])
  272. elif isinstance(axes, (np.ndarray, ABCIndexClass)):
  273. return axes.ravel()
  274. return np.array(axes)
  275. def _set_ticks_props(axes, xlabelsize=None, xrot=None, ylabelsize=None, yrot=None):
  276. import matplotlib.pyplot as plt
  277. for ax in _flatten(axes):
  278. if xlabelsize is not None:
  279. plt.setp(ax.get_xticklabels(), fontsize=xlabelsize)
  280. if xrot is not None:
  281. plt.setp(ax.get_xticklabels(), rotation=xrot)
  282. if ylabelsize is not None:
  283. plt.setp(ax.get_yticklabels(), fontsize=ylabelsize)
  284. if yrot is not None:
  285. plt.setp(ax.get_yticklabels(), rotation=yrot)
  286. return axes
  287. def _get_all_lines(ax):
  288. lines = ax.get_lines()
  289. if hasattr(ax, "right_ax"):
  290. lines += ax.right_ax.get_lines()
  291. if hasattr(ax, "left_ax"):
  292. lines += ax.left_ax.get_lines()
  293. return lines
  294. def _get_xlim(lines):
  295. left, right = np.inf, -np.inf
  296. for l in lines:
  297. x = l.get_xdata(orig=False)
  298. left = min(np.nanmin(x), left)
  299. right = max(np.nanmax(x), right)
  300. return left, right