Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/pandas/plotting/_misc.py: 41%
73 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1from __future__ import annotations
3from contextlib import contextmanager
4from typing import (
5 TYPE_CHECKING,
6 Iterator,
7)
9from pandas.plotting._core import _get_plot_backend
11if TYPE_CHECKING: 11 ↛ 12line 11 didn't jump to line 12, because the condition on line 11 was never true
12 from matplotlib.axes import Axes
13 from matplotlib.figure import Figure
14 import numpy as np
16 from pandas import (
17 DataFrame,
18 Series,
19 )
22def table(ax, data, rowLabels=None, colLabels=None, **kwargs):
23 """
24 Helper function to convert DataFrame and Series to matplotlib.table.
26 Parameters
27 ----------
28 ax : Matplotlib axes object
29 data : DataFrame or Series
30 Data for table contents.
31 **kwargs
32 Keyword arguments to be passed to matplotlib.table.table.
33 If `rowLabels` or `colLabels` is not specified, data index or column
34 name will be used.
36 Returns
37 -------
38 matplotlib table object
39 """
40 plot_backend = _get_plot_backend("matplotlib")
41 return plot_backend.table(
42 ax=ax, data=data, rowLabels=None, colLabels=None, **kwargs
43 )
46def register() -> None:
47 """
48 Register pandas formatters and converters with matplotlib.
50 This function modifies the global ``matplotlib.units.registry``
51 dictionary. pandas adds custom converters for
53 * pd.Timestamp
54 * pd.Period
55 * np.datetime64
56 * datetime.datetime
57 * datetime.date
58 * datetime.time
60 See Also
61 --------
62 deregister_matplotlib_converters : Remove pandas formatters and converters.
63 """
64 plot_backend = _get_plot_backend("matplotlib")
65 plot_backend.register()
68def deregister() -> None:
69 """
70 Remove pandas formatters and converters.
72 Removes the custom converters added by :func:`register`. This
73 attempts to set the state of the registry back to the state before
74 pandas registered its own units. Converters for pandas' own types like
75 Timestamp and Period are removed completely. Converters for types
76 pandas overwrites, like ``datetime.datetime``, are restored to their
77 original value.
79 See Also
80 --------
81 register_matplotlib_converters : Register pandas formatters and converters
82 with matplotlib.
83 """
84 plot_backend = _get_plot_backend("matplotlib")
85 plot_backend.deregister()
88def scatter_matrix(
89 frame: DataFrame,
90 alpha: float = 0.5,
91 figsize: tuple[float, float] | None = None,
92 ax: Axes | None = None,
93 grid: bool = False,
94 diagonal: str = "hist",
95 marker: str = ".",
96 density_kwds=None,
97 hist_kwds=None,
98 range_padding: float = 0.05,
99 **kwargs,
100) -> np.ndarray:
101 """
102 Draw a matrix of scatter plots.
104 Parameters
105 ----------
106 frame : DataFrame
107 alpha : float, optional
108 Amount of transparency applied.
109 figsize : (float,float), optional
110 A tuple (width, height) in inches.
111 ax : Matplotlib axis object, optional
112 grid : bool, optional
113 Setting this to True will show the grid.
114 diagonal : {'hist', 'kde'}
115 Pick between 'kde' and 'hist' for either Kernel Density Estimation or
116 Histogram plot in the diagonal.
117 marker : str, optional
118 Matplotlib marker type, default '.'.
119 density_kwds : keywords
120 Keyword arguments to be passed to kernel density estimate plot.
121 hist_kwds : keywords
122 Keyword arguments to be passed to hist function.
123 range_padding : float, default 0.05
124 Relative extension of axis range in x and y with respect to
125 (x_max - x_min) or (y_max - y_min).
126 **kwargs
127 Keyword arguments to be passed to scatter function.
129 Returns
130 -------
131 numpy.ndarray
132 A matrix of scatter plots.
134 Examples
135 --------
137 .. plot::
138 :context: close-figs
140 >>> df = pd.DataFrame(np.random.randn(1000, 4), columns=['A','B','C','D'])
141 >>> pd.plotting.scatter_matrix(df, alpha=0.2)
142 array([[<AxesSubplot: xlabel='A', ylabel='A'>,
143 <AxesSubplot: xlabel='B', ylabel='A'>,
144 <AxesSubplot: xlabel='C', ylabel='A'>,
145 <AxesSubplot: xlabel='D', ylabel='A'>],
146 [<AxesSubplot: xlabel='A', ylabel='B'>,
147 <AxesSubplot: xlabel='B', ylabel='B'>,
148 <AxesSubplot: xlabel='C', ylabel='B'>,
149 <AxesSubplot: xlabel='D', ylabel='B'>],
150 [<AxesSubplot: xlabel='A', ylabel='C'>,
151 <AxesSubplot: xlabel='B', ylabel='C'>,
152 <AxesSubplot: xlabel='C', ylabel='C'>,
153 <AxesSubplot: xlabel='D', ylabel='C'>],
154 [<AxesSubplot: xlabel='A', ylabel='D'>,
155 <AxesSubplot: xlabel='B', ylabel='D'>,
156 <AxesSubplot: xlabel='C', ylabel='D'>,
157 <AxesSubplot: xlabel='D', ylabel='D'>]], dtype=object)
158 """
159 plot_backend = _get_plot_backend("matplotlib")
160 return plot_backend.scatter_matrix(
161 frame=frame,
162 alpha=alpha,
163 figsize=figsize,
164 ax=ax,
165 grid=grid,
166 diagonal=diagonal,
167 marker=marker,
168 density_kwds=density_kwds,
169 hist_kwds=hist_kwds,
170 range_padding=range_padding,
171 **kwargs,
172 )
175def radviz(
176 frame: DataFrame,
177 class_column: str,
178 ax: Axes | None = None,
179 color: list[str] | tuple[str, ...] | None = None,
180 colormap=None,
181 **kwds,
182) -> Axes:
183 """
184 Plot a multidimensional dataset in 2D.
186 Each Series in the DataFrame is represented as a evenly distributed
187 slice on a circle. Each data point is rendered in the circle according to
188 the value on each Series. Highly correlated `Series` in the `DataFrame`
189 are placed closer on the unit circle.
191 RadViz allow to project a N-dimensional data set into a 2D space where the
192 influence of each dimension can be interpreted as a balance between the
193 influence of all dimensions.
195 More info available at the `original article
196 <https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.135.889>`_
197 describing RadViz.
199 Parameters
200 ----------
201 frame : `DataFrame`
202 Object holding the data.
203 class_column : str
204 Column name containing the name of the data point category.
205 ax : :class:`matplotlib.axes.Axes`, optional
206 A plot instance to which to add the information.
207 color : list[str] or tuple[str], optional
208 Assign a color to each category. Example: ['blue', 'green'].
209 colormap : str or :class:`matplotlib.colors.Colormap`, default None
210 Colormap to select colors from. If string, load colormap with that
211 name from matplotlib.
212 **kwds
213 Options to pass to matplotlib scatter plotting method.
215 Returns
216 -------
217 class:`matplotlib.axes.Axes`
219 See Also
220 --------
221 plotting.andrews_curves : Plot clustering visualization.
223 Examples
224 --------
226 .. plot::
227 :context: close-figs
229 >>> df = pd.DataFrame(
230 ... {
231 ... 'SepalLength': [6.5, 7.7, 5.1, 5.8, 7.6, 5.0, 5.4, 4.6, 6.7, 4.6],
232 ... 'SepalWidth': [3.0, 3.8, 3.8, 2.7, 3.0, 2.3, 3.0, 3.2, 3.3, 3.6],
233 ... 'PetalLength': [5.5, 6.7, 1.9, 5.1, 6.6, 3.3, 4.5, 1.4, 5.7, 1.0],
234 ... 'PetalWidth': [1.8, 2.2, 0.4, 1.9, 2.1, 1.0, 1.5, 0.2, 2.1, 0.2],
235 ... 'Category': [
236 ... 'virginica',
237 ... 'virginica',
238 ... 'setosa',
239 ... 'virginica',
240 ... 'virginica',
241 ... 'versicolor',
242 ... 'versicolor',
243 ... 'setosa',
244 ... 'virginica',
245 ... 'setosa'
246 ... ]
247 ... }
248 ... )
249 >>> pd.plotting.radviz(df, 'Category')
250 <AxesSubplot: xlabel='y(t)', ylabel='y(t + 1)'>
251 """
252 plot_backend = _get_plot_backend("matplotlib")
253 return plot_backend.radviz(
254 frame=frame,
255 class_column=class_column,
256 ax=ax,
257 color=color,
258 colormap=colormap,
259 **kwds,
260 )
263def andrews_curves(
264 frame: DataFrame,
265 class_column: str,
266 ax: Axes | None = None,
267 samples: int = 200,
268 color: list[str] | tuple[str, ...] | None = None,
269 colormap=None,
270 **kwargs,
271) -> Axes:
272 """
273 Generate a matplotlib plot for visualising clusters of multivariate data.
275 Andrews curves have the functional form:
277 f(t) = x_1/sqrt(2) + x_2 sin(t) + x_3 cos(t) +
278 x_4 sin(2t) + x_5 cos(2t) + ...
280 Where x coefficients correspond to the values of each dimension and t is
281 linearly spaced between -pi and +pi. Each row of frame then corresponds to
282 a single curve.
284 Parameters
285 ----------
286 frame : DataFrame
287 Data to be plotted, preferably normalized to (0.0, 1.0).
288 class_column : Name of the column containing class names
289 ax : matplotlib axes object, default None
290 samples : Number of points to plot in each curve
291 color : list or tuple, optional
292 Colors to use for the different classes.
293 colormap : str or matplotlib colormap object, default None
294 Colormap to select colors from. If string, load colormap with that name
295 from matplotlib.
296 **kwargs
297 Options to pass to matplotlib plotting method.
299 Returns
300 -------
301 class:`matplotlip.axis.Axes`
303 Examples
304 --------
306 .. plot::
307 :context: close-figs
309 >>> df = pd.read_csv(
310 ... 'https://raw.github.com/pandas-dev/'
311 ... 'pandas/main/pandas/tests/io/data/csv/iris.csv'
312 ... )
313 >>> pd.plotting.andrews_curves(df, 'Name')
314 <AxesSubplot: title={'center': 'width'}>
315 """
316 plot_backend = _get_plot_backend("matplotlib")
317 return plot_backend.andrews_curves(
318 frame=frame,
319 class_column=class_column,
320 ax=ax,
321 samples=samples,
322 color=color,
323 colormap=colormap,
324 **kwargs,
325 )
328def bootstrap_plot(
329 series: Series,
330 fig: Figure | None = None,
331 size: int = 50,
332 samples: int = 500,
333 **kwds,
334) -> Figure:
335 """
336 Bootstrap plot on mean, median and mid-range statistics.
338 The bootstrap plot is used to estimate the uncertainty of a statistic
339 by relaying on random sampling with replacement [1]_. This function will
340 generate bootstrapping plots for mean, median and mid-range statistics
341 for the given number of samples of the given size.
343 .. [1] "Bootstrapping (statistics)" in \
344 https://en.wikipedia.org/wiki/Bootstrapping_%28statistics%29
346 Parameters
347 ----------
348 series : pandas.Series
349 Series from where to get the samplings for the bootstrapping.
350 fig : matplotlib.figure.Figure, default None
351 If given, it will use the `fig` reference for plotting instead of
352 creating a new one with default parameters.
353 size : int, default 50
354 Number of data points to consider during each sampling. It must be
355 less than or equal to the length of the `series`.
356 samples : int, default 500
357 Number of times the bootstrap procedure is performed.
358 **kwds
359 Options to pass to matplotlib plotting method.
361 Returns
362 -------
363 matplotlib.figure.Figure
364 Matplotlib figure.
366 See Also
367 --------
368 DataFrame.plot : Basic plotting for DataFrame objects.
369 Series.plot : Basic plotting for Series objects.
371 Examples
372 --------
373 This example draws a basic bootstrap plot for a Series.
375 .. plot::
376 :context: close-figs
378 >>> s = pd.Series(np.random.uniform(size=100))
379 >>> pd.plotting.bootstrap_plot(s)
380 <Figure size 640x480 with 6 Axes>
381 """
382 plot_backend = _get_plot_backend("matplotlib")
383 return plot_backend.bootstrap_plot(
384 series=series, fig=fig, size=size, samples=samples, **kwds
385 )
388def parallel_coordinates(
389 frame: DataFrame,
390 class_column: str,
391 cols: list[str] | None = None,
392 ax: Axes | None = None,
393 color: list[str] | tuple[str, ...] | None = None,
394 use_columns: bool = False,
395 xticks: list | tuple | None = None,
396 colormap=None,
397 axvlines: bool = True,
398 axvlines_kwds=None,
399 sort_labels: bool = False,
400 **kwargs,
401) -> Axes:
402 """
403 Parallel coordinates plotting.
405 Parameters
406 ----------
407 frame : DataFrame
408 class_column : str
409 Column name containing class names.
410 cols : list, optional
411 A list of column names to use.
412 ax : matplotlib.axis, optional
413 Matplotlib axis object.
414 color : list or tuple, optional
415 Colors to use for the different classes.
416 use_columns : bool, optional
417 If true, columns will be used as xticks.
418 xticks : list or tuple, optional
419 A list of values to use for xticks.
420 colormap : str or matplotlib colormap, default None
421 Colormap to use for line colors.
422 axvlines : bool, optional
423 If true, vertical lines will be added at each xtick.
424 axvlines_kwds : keywords, optional
425 Options to be passed to axvline method for vertical lines.
426 sort_labels : bool, default False
427 Sort class_column labels, useful when assigning colors.
428 **kwargs
429 Options to pass to matplotlib plotting method.
431 Returns
432 -------
433 class:`matplotlib.axis.Axes`
435 Examples
436 --------
438 .. plot::
439 :context: close-figs
441 >>> df = pd.read_csv(
442 ... 'https://raw.github.com/pandas-dev/'
443 ... 'pandas/main/pandas/tests/io/data/csv/iris.csv'
444 ... )
445 >>> pd.plotting.parallel_coordinates(
446 ... df, 'Name', color=('#556270', '#4ECDC4', '#C7F464')
447 ... )
448 <AxesSubplot: xlabel='y(t)', ylabel='y(t + 1)'>
449 """
450 plot_backend = _get_plot_backend("matplotlib")
451 return plot_backend.parallel_coordinates(
452 frame=frame,
453 class_column=class_column,
454 cols=cols,
455 ax=ax,
456 color=color,
457 use_columns=use_columns,
458 xticks=xticks,
459 colormap=colormap,
460 axvlines=axvlines,
461 axvlines_kwds=axvlines_kwds,
462 sort_labels=sort_labels,
463 **kwargs,
464 )
467def lag_plot(series: Series, lag: int = 1, ax: Axes | None = None, **kwds) -> Axes:
468 """
469 Lag plot for time series.
471 Parameters
472 ----------
473 series : Time series
474 lag : lag of the scatter plot, default 1
475 ax : Matplotlib axis object, optional
476 **kwds
477 Matplotlib scatter method keyword arguments.
479 Returns
480 -------
481 class:`matplotlib.axis.Axes`
483 Examples
484 --------
486 Lag plots are most commonly used to look for patterns in time series data.
488 Given the following time series
490 .. plot::
491 :context: close-figs
493 >>> np.random.seed(5)
494 >>> x = np.cumsum(np.random.normal(loc=1, scale=5, size=50))
495 >>> s = pd.Series(x)
496 >>> s.plot()
497 <AxesSubplot: xlabel='Midrange'>
499 A lag plot with ``lag=1`` returns
501 .. plot::
502 :context: close-figs
504 >>> pd.plotting.lag_plot(s, lag=1)
505 <AxesSubplot: xlabel='y(t)', ylabel='y(t + 1)'>
506 """
507 plot_backend = _get_plot_backend("matplotlib")
508 return plot_backend.lag_plot(series=series, lag=lag, ax=ax, **kwds)
511def autocorrelation_plot(series: Series, ax: Axes | None = None, **kwargs) -> Axes:
512 """
513 Autocorrelation plot for time series.
515 Parameters
516 ----------
517 series : Time series
518 ax : Matplotlib axis object, optional
519 **kwargs
520 Options to pass to matplotlib plotting method.
522 Returns
523 -------
524 class:`matplotlib.axis.Axes`
526 Examples
527 --------
529 The horizontal lines in the plot correspond to 95% and 99% confidence bands.
531 The dashed line is 99% confidence band.
533 .. plot::
534 :context: close-figs
536 >>> spacing = np.linspace(-9 * np.pi, 9 * np.pi, num=1000)
537 >>> s = pd.Series(0.7 * np.random.rand(1000) + 0.3 * np.sin(spacing))
538 >>> pd.plotting.autocorrelation_plot(s)
539 <AxesSubplot: title={'center': 'width'}, xlabel='Lag', ylabel='Autocorrelation'>
540 """
541 plot_backend = _get_plot_backend("matplotlib")
542 return plot_backend.autocorrelation_plot(series=series, ax=ax, **kwargs)
545class _Options(dict):
546 """
547 Stores pandas plotting options.
549 Allows for parameter aliasing so you can just use parameter names that are
550 the same as the plot function parameters, but is stored in a canonical
551 format that makes it easy to breakdown into groups later.
552 """
554 # alias so the names are same as plotting method parameter names
555 _ALIASES = {"x_compat": "xaxis.compat"}
556 _DEFAULT_KEYS = ["xaxis.compat"]
558 def __init__(self, deprecated: bool = False) -> None:
559 self._deprecated = deprecated
560 super().__setitem__("xaxis.compat", False)
562 def __getitem__(self, key):
563 key = self._get_canonical_key(key)
564 if key not in self:
565 raise ValueError(f"{key} is not a valid pandas plotting option")
566 return super().__getitem__(key)
568 def __setitem__(self, key, value) -> None:
569 key = self._get_canonical_key(key)
570 super().__setitem__(key, value)
572 def __delitem__(self, key) -> None:
573 key = self._get_canonical_key(key)
574 if key in self._DEFAULT_KEYS:
575 raise ValueError(f"Cannot remove default parameter {key}")
576 super().__delitem__(key)
578 def __contains__(self, key) -> bool:
579 key = self._get_canonical_key(key)
580 return super().__contains__(key)
582 def reset(self) -> None:
583 """
584 Reset the option store to its initial state
586 Returns
587 -------
588 None
589 """
590 # error: Cannot access "__init__" directly
591 self.__init__() # type: ignore[misc]
593 def _get_canonical_key(self, key):
594 return self._ALIASES.get(key, key)
596 @contextmanager
597 def use(self, key, value) -> Iterator[_Options]:
598 """
599 Temporarily set a parameter value using the with statement.
600 Aliasing allowed.
601 """
602 old_value = self[key]
603 try:
604 self[key] = value
605 yield self
606 finally:
607 self[key] = old_value
610plot_params = _Options()