mypythontools.plots package

Plot data. There is only one main function plot. Check ‘plot’ documentation for how to use it.

mypythontools.plots.plot(df: pd.DataFrame, plot_library: Literal['plotly', 'matplotlib'] = 'plotly', title: str = 'Plot', legend: bool = True, y_axis_name='Values', blue_column='', black_column='', grey_area: None | list[str] = None, save_path: None | PathLike = None, return_div: bool = False, show: bool = True) None | str[source]

Plots the data.

Plotly or matplotlib can be used. It is possible to highlight two columns with different formatting. It is usually used for time series visualization, but it can be used for different use case of course.

Parameters
  • df (pd.DataFrame) – Data to be plotted.

  • plot_library (Literal['plotly', 'matplotlib'], optional) – ‘plotly’ or ‘matplotlib’ Defaults to “plotly”.

  • legend (bool, optional) – Whether display legend or not. Defaults to True.

  • blue_column (str, optional) – Column name that will be formatted differently (blue). Defaults to “”.

  • black_column (str, optional) – Column name that will be formatted differently (black, wider). And is surrounded by grey_area. Can be empty (if grey_area is None). Defaults to “”.

  • grey_area (None | list[str]), optional) – Whether to show grey area surrounding the black_column. Can be None, or list of [‘lower_bound_column’, ‘upper_bound_column’]. Both columns has to be in df. Defaults to None.

  • save_path (None | PathLike, optional) – Whether save the plot. If False or “”, do not save, if Path as str path, save to defined path. If “DESKTOP” save to desktop. Defaults to None.

  • return_div (bool, optional) – If True, return html div with plot as string. If False, just plot and do not return. Defaults to False.

  • show (bool, optional) – Can be evaluated, but not shown (testing reasons). Defaults to True.

Returns

Only if return_div is True, else None.

Return type

None | str

Examples

Plot DataFrame with

>>> import pandas as pd
>>> df = pd.DataFrame([[None, None, 1], [None, None, 2], [3, 3, 6], [3, 2.5, 4]])
>>> plot(df, show=False)  # Show False just for testing reasons

Example of filling grey area between columns

>>> df = pd.DataFrame(
...     [[None, None, 0], [None, None, 2], [3, 3, 3], [2, 5, 4]], columns=["0", "1", "2"]
... )
>>> plot(df, grey_area=["0", "1"], show=False)

You can use matplotlib

>>> plot(df, plot_library="matplotlib")
Raises

KeyError – If defined column not found in DataFrame