100 days of code: Day 43 - scatter

Last modified on 2020-07-22

Source: 100-days-of-code.

My dates_weights.py program has acquired the --scatter option and the ability to generate scatter plot graphs

$ ./dates_weights.py -h
usage: dates_weights.py [-h] [-o OUTPUT] [-q] [-r] [-s] source

Retrieve weight measurements and dates they were recorded from <source>.

positional arguments:
  source                data source file

optional arguments:
  -h, --help            show this help message and exit
  -o OUTPUT, --output OUTPUT
                        save data to file
  -q, --quiet           do not display output
  -r, --raw             display list of matching patterns
  -s, --scatter         display a scatter plot

Building on yesterday’s play with building dataframes using pandas I use the plotly.express module for the graphing.

Add dict_to_tab() to generate the dataframe and make_scatter() to class DateTag

"""A class that represents a regex pattern and its associated tag and date."""

import re
import json
import pandas as pd
import datetime as dt
import plotly.express as px

class DateTag:
    """Search for a regex pattern and its associated tag and date."""

[...]

    def dict_to_tab(self, dictionary, date_column, measure_column):
        """Convert dictionary to tabular data."""
        my_dict = dictionary
        col_1 = date_column
        col_2 = measure_column
        d = '%Y-%m-%d'
        # Display all rows (do not truncate).
        pd.set_option('display.max_rows', None)
        # Convert dictionary to dataframe
        df = pd.DataFrame(list(my_dict.items()),columns = [col_1,col_2])
        # Create date objects in the syntax `datetime.date(Y, M, D)`
        df[col_1] = df[col_1].apply(lambda x: dt.datetime.strptime(x,d))
        return df

    def make_scatter(self, data_frame, title, xlabel, ylabel):
        """Make a scatter plot from <data_frame>."""
        df = data_frame
        tl = title
        xl = xlabel
        yl = ylabel
        fig = px.scatter(df, x=xl, y=yl, title=tl)
        fig.show()

Now when I run dates_weights.py it scans the specified file for matching patterns and displays:

Happy hacking!