Visualizations - Basics#

This tutorial focuses on working with general principles of visualizations in plans.

Notebook setup#

For users running this tutorial as a Jupyter Notebook, this cell must be executed first:

import sys
from pathlib import Path
import pprint
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Install `plans` in `google.colab`.
# Use `pip install plans` for other environments.

if "google.colab" in sys.modules:
    import os
    os.system(f"{sys.executable} -m pip install -q plans")

# This avoids warnings related to uninstalled fonts
import logging
# Set the matplotlib font manager logger to only show errors (hides warnings)
logging.getLogger('matplotlib.font_manager').setLevel(logging.ERROR)

# define output folder
OUTPUT_DIR = Path("outputs/visualizations")
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
print(f"Outputs will be saved to: ./{OUTPUT_DIR}")
Outputs will be saved to: ./outputs/visualizations

Data setup#

The cell bellow creates a synthetic time series and loads it to a plans.datasets.TimeSeries object.

See also

Check out more about TimeSeries on the Time series data - Basics tutorial.

from plans.datasets import TimeSeries

# Set the ts variable
ts = TimeSeries(name="Testing", alias="tst")

# make synthetic (Trend, Cycle and Remainder) time-series
df = ts.generate(start="2020-01-01", end="2022-01-01", freq="D", seed=1)

# Export CSV file
file_csv = OUTPUT_DIR / "time_series.csv"
df.to_csv(file_csv, sep=";", index="False")
print(f"Saved to: {file_csv}")

# Load data from CSV
ts.load_data(
    file_data=file_csv,  # file path
    input_dtfield="datetime", # name of datetime field
    input_varfield="v",  # name of variable
    in_sep=";",  # input separator
)
Saved to: outputs/visualizations/time_series.csv

Standard visualization#

Get the standard visual using the .view() method

ts.view()
../_images/6b0ca560b58bd842bf51dc7f92b227d30721291cb41d23e7a3e4cb730e61ef1b.png

Fine-tuning#

Fine-tune the visualization by editing the .view_specs attribute dictionary

# color of the main line
ts.view_specs["color"] = "blue"
ts.view_specs["color_hist"] = "green"

# Labels
ts.view_specs["ylabel"] = f"Level (cm)"
ts.view_specs["xlabel"] = "Date"

# Titles
ts.view_specs["title"] = "Hello! This is just a tutorial!"
ts.view_specs["subtitle_data"] = "a. Time Series"
ts.view_specs["subtitle_hist"] = "b. Histogram"
ts.view_specs["subtitle_cdf"] = "c. CDF"

# Data Y Axis range
ts.view_specs["range"] = [0, 30.0]

# Number of dates in the X axis (TimeSeries only)
ts.view_specs["n_dates"] = 7

# Call view() again
ts.view()
../_images/e12700b6acde3de0d926c1e759d662050762f2930f44bfc00b3dd77e04f6942d.png

List all view specs keys for the current object:

pprint.pp(ts.view_specs.keys())
dict_keys(['style', 'gs_wspace', 'gs_hspace', 'gs_left', 'gs_right', 'gs_bottom', 'gs_top', 'folder', 'filename', 'fig_format', 'dpi', 'title', 'xvar', 'yvar', 'xlabel', 'ylabel', 'color', 'xmin', 'xmax', 'ymin', 'ymax', 'layout', 'data_plot', 'subtitle_data', 'subtitle_box', 'subtitle_hist', 'subtitle_cdf', 'subtitle_qq', 'yvar_field', 'xlabel_cdf', 'xlabel_qq', 'data_label', 'data_legend', 'color_hist', 'color_cdf', 'color_mean', 'color_mean_text', 'scheme_cmap', 'n_classes_cmap', 'cmap', 'alpha', 'alpha_hist', 'alpha_cdf', 'range', 'range_x', 'plot_mean', 'plot_mean_data', 'linestyle_mean', 'pad_mean', 'plot_mean_label', 'hist_density', 'bins', 'bins_density', 'ax_data', 'ax_hist', 'ax_cdf', 'x_stats', 'x_stats_sep', 'y_stats', 'zorder_data', 'zorder_histh', 'zorder_cdf', 'scatter_factor', 'colorize_scatter', 'box_width', 'box_showfliers', 'subtitle_epochs', 'color_aux', 'color_fill', 'color_eva', 'legend_eva', 'alpha_aux', 'alpha_fill', 'fill', 'fill_only', 'color_gaps', 'alpha_gaps', 'linewidth_gaps', 'xmax_aux', 'pad_x', 'linestyle', 'marker', 'marker_eva', 'drawstyle', 'n_bins', 'n_dates', 'format_dates', 'include_eva'])

Reset view specs to default values:

ts._set_view_specs()

Save to image file#

Save to an image file the visual by setting show=False. This will save in the same folder of the data with a standard name.

ts.view(show=False)

A custom image export can be achieved by changing the folder, filename, and fig_format parameters of the .view_specs

ts.view_specs["folder"] = OUTPUT_DIR
ts.view_specs["filename"] = "Testing - blue - 2"
ts.view_specs["fig_format"] = "jpeg" # instead of jpg

ts.view(show=False)

Visual style#

The visual overall style can be switched via the style key. The default style is wien.

List all available styles:

from plans.viewer import FIG_STYLES
pprint.pp(FIG_STYLES.keys())
dict_keys(['bare', 'wien', 'wien3d', 'wien-light', 'wien-clean', 'seaborn', 'dark'])
ts.view_specs["style"] = "wien-light"
ts.view()
../_images/2dfcdfd0e60a098068b1539abfc23eb045feec255784418feffdbf8135af7250.png
ts.view_specs["style"] = "wien-clean"
ts.view()
../_images/96e2d6bb424633e0b99215129b02ecc30686111e2031e54a8b46859ec4d606ac.png
ts.view_specs["style"] = "dark"
ts.view()
../_images/c5b4fb3df7fa9e9ef3f01c0fcd50dc790270c11efc8c5a033a871fd2926fbdfa.png
ts.view_specs["style"] = "seaborn"
ts.view()
../_images/864dc790b7b9029bc43eaa04e6e77a8dd19a7e471df53677c01ec354d75d25e4.png
ts.view_specs["style"] = "bare"
ts.view()
../_images/88cc928e77c18bc79e005212808e29887e387948f88ab96b627e9c3faaf61adf.png
# reset view specs
ts._set_view_specs()

Visual layout#

The visual layout mode can be changed via the mode key. The default mode is full.

Warning

Layout options can vary according to the object

ts.view_specs["layout"] = "full"
ts.view()
../_images/620396bca98bd71b474a1aae08cf721e4002b4c338d50f019f0192e6e0435918.png
ts.view_specs["layout"] = "mini"
ts.view()
../_images/06adf913e227f9375e77a182b189cb1f14a46c10aee559f320d6caedeb8a6e91.png
ts.view_specs["layout"] = "simple"
ts.view()
../_images/4b47bfbf6a50855254f3f98b2fcd3ee214c29b1aba83d1929ec173650f236b6e.png