*Draw a pie chart as an inset (in absolute figure coordinates) within the given axes at the specified data coordinates. What this solves is the issue of y-axis and x-axis scaling being different, which distorts the pie chart when drawn directly on the axes.
Args:
ax (plt.Axes): The axis on which to draw the pie chart.
x (float): The x-coordinate in data coordinates where the pie chart's center will be placed.
y (float): The y-coordinate in data coordinates where the pie chart's center will be placed.
data (List[float]): The data values to be represented in the pie chart.
size (float): The size (radius) of the pie chart in axes-relative coordinates.
palette (str): The color palette to use for the pie slices.
Returns:
List[plt.Patch]: A list of wedge objects representing the pie chart slices.*
*Prepare the diet log data for plotting meals and/or daily summaries.
Args:
diet_log (pd.DataFrame): The dataframe containing the diet log data, with columns for timestamps, nutrients, and other measurements.
participant_id (Optional[int]): The participant's ID to filter the diet log. If None, no filtering is done. Default is None.
array_index (Optional[int]): The array index to filter the diet log. If None, no filtering is done. Default is None.
time_range (Optional[Tuple[str, str]]): A tuple of strings representing the start and end dates for filtering the data. Format should be 'YYYY-MM-DD'. Default is None.
label (str): The name of the column in `diet_log` representing the labels for each meal. Default is 'short_food_name'.
return_meals (bool): If True, includes individual meals in the plot. Default is True.
return_summary (bool): If True, includes a daily summary in the plot. Default is False.
y_include (List[str]): A list of nutrients (regex) to include in the plot. Default is None.
y_exclude (List[str]): A list of nutrients (regex) to exclude from the plot. Default is None.
agg_units (dict): A dictionary mapping nutrient units to aggregation functions.
x_col (str): The name of the column in `diet_log` representing the x-axis variable, such as timestamps. Default is 'collection_timestamp'.
Returns:
pd.DataFrame: A dataframe containing the prepared data for plotting.*
*Plot a lollipop chart with pie charts representing nutrient composition for each meal.
NOTE: The y-axis is scaled to match the units of the x-axis, to avoid distortion of the pie charts. Due to scaling, if you intend to change xlim after plotting, you must also provide date_range. Use the second_y of g.plot() option to plot it with other y-axis data.
Args:
diet_log (pd.DataFrame): The dataframe containing the diet log data, with columns for timestamps, nutrients, and other measurements.
x (str): The name of the column in `diet_log` representing the x-axis variable, such as timestamps. Default is 'collection_timestamp'.
y (str): The name of the column in `diet_log` representing the y-axis variable, such as calories. Default is 'calories_kcal'.
size (str): The name of the column in `diet_log` representing the size of the pie charts. Default is 'total_g'.
label (str): The name of the column in `diet_log` representing the labels for each meal. Default is 'short_food_name'.
participant_id (Optional[int]): The participant's ID to filter the diet log. If None, no filtering is done. Default is None.
time_range (Optional[Tuple[str, str]]): A tuple of strings representing the start and end dates for filtering the data. Format should be 'YYYY-MM-DD'. Default is None.
meals (bool): If True, includes individual meals in the plot. Default is True.
summary (bool): If True, includes a daily summary in the plot. Default is False.
nut_include (List[str]): A list of nutrients to include in the plot. Default is None.
nut_exclude (List[str]): A list of nutrients to exclude from the plot. Default is None.
legend (bool): If True, includes a legend in the plot. Default is True.
size_scale (float): The scaling factor for the size of the pie charts. Default is 5.
palette (str): The color palette to use for the pie slices. Default is DEFAULT_PALETTTE.
alpha (float): The transparency of the pie slices. Default is 0.7.
ax (Optional[plt.Axes]): The Matplotlib axis on which to plot the lollipop chart. If None, a new axis is created. Default is None.
figsize (Tuple[float, float]): The size of the figure to create. Default is (12, 6).
Returns:
plt.Axes: The Matplotlib axis on which the chart was plotted.*
*Plot a stacked bar chart representing nutrient intake for each meal over time.
Args:
diet_log (pd.DataFrame): The dataframe containing the diet log data, with columns for timestamps, nutrients, and other measurements.
x (str): The name of the column in `diet_log` representing the x-axis variable, such as timestamps. Default is 'collection_timestamp'.
label (str): The name of the column in `diet_log` representing the labels for each meal. Default is 'short_food_name'.
participant_id (Optional[int]): The participant's ID to filter the diet log. If None, no filtering is done. Default is None.
array_index (Optional[int]): The array index to filter the diet log. If None, no filtering is done. Default is None.
time_range (Optional[Tuple[str, str]]): A tuple of strings representing the start and end dates for filtering the data. Format should be 'YYYY-MM-DD'. Default is None.
meals (bool): If True, includes individual meals in the plot. Default is True.
summary (bool): If True, includes a daily summary in the plot. Default is False.
nut_include (List[str]): A list of nutrients to include in the plot. Default is None.
nut_exclude (List[str]): A list of nutrients to exclude from the plot. Default is None.
agg_units (dict): A dictionary mapping nutrient units to aggregation functions. Only nutrients with units in this dictionary are plotted.
legend (bool): If True, includes a legend in the plot. Default is True.
bar_width (np.timedelta64): The width of the bars representing each meal on the time axis. Default is 15 minutes.
palette (str): The color palette to use for the stacked bars.
alpha (float): The transparency of the stacked bars. Default is 0.7.
ax (Optional[plt.Axes]): The Matplotlib axis on which to plot the bar chart. If None, a new axis is created. Default is None.
figsize (Tuple[float, float]): The size of the figure to create. Default is (14, 3).
Returns:
None: The function creates a stacked bar chart on the specified or newly created axis.*
plot_meals_hbars (diet_log:pandas.core.frame.DataFrame,
x:str='collection_timestamp',
y:str='short_food_category', size:str='weight_g',
hue:str='short_food_category', participant_id:int=None,
array_index:int=None, time_range:Tuple[str,str]=None,
y_include:List[str]=None, y_exclude:List[str]=None,
rename_categories:dict={'beef, veal, lamb, and other
meat products': 'meat products', 'milk, cream cheese
and yogurts': 'milk products', 'nuts, seeds, and
products': 'nuts and seeds', 'eggs and their products':
'eggs', 'pulses and products': 'pulses', 'fruit juices
and soft drinks': 'juices and soft drinks', 'low
calories and diet drinks': 'low cal. drinks', 'poultry
and its products': 'poultry', 'pasta, grains and side
dishes': 'grains', 'industrialized vegetarian food
ready to eat': 'industrialized veg.'},
legend:bool=True, size_legend:List[int]=[100, 200,
500], size_scale:float=5, palette:str='muted',
alpha:float=0.7, ax:matplotlib.axes._axes.Axes=None,
figsize:Tuple[float,float]=(12, 6))
*Plot a diet chart with bars representing meals and their size over time.
Args:
diet_log (pd.DataFrame): The dataframe containing the diet log data, with columns for timestamps, nutrients, and other measurements.
x (str): The name of the column in `diet_log` representing the x-axis variable, such as timestamps. Default is 'collection_timestamp'.
y (str): The name of the column in `diet_log` representing the y-axis variable, such as food categories. Default is 'short_food_category'.
size (str): The name of the column in `diet_log` representing the size of the bars. Default is 'weight_g'.
hue (str): The name of the column in `diet_log` representing the color of the bars. Default is 'short_food_category'.
participant_id (Optional[int]): The participant's ID to filter the diet log. If None, no filtering is done. Default is None.
time_range (Optional[Tuple[str, str]]): A tuple of strings representing the start and end dates for filtering the data. Format should be 'YYYY-MM-DD'. Default is None.
y_include (List[str]): A list of strings representing the categories to include in the plot. Default is None.
y_exclude (List[str]): A list of strings representing the categories to exclude from the plot. Default is None.
rename_categories (dict): A dictionary mapping original food categories to shorter names. Default is SHORT_FOOD_CATEGORIES.
legend (bool): If True, includes a legend in the plot. Default is True.
size_legend (List[int]): A list of integers representing the sizes to include in the size legend. Default is [100, 200, 500].
size_scale (float): The scaling factor for the size of the bars. Default is 5.
palette (str): The palette to use for the bars.
alpha (float): The transparency of the bars. Default is 0.7.
ax (Optional[plt.Axes]): The Matplotlib axis on which to plot the lollipop chart. If None, a new axis is created. Default is None.
figsize (Tuple[float, float]): The size of the figure to create. Default is (12, 6).
Returns:
plt.Axes: The Matplotlib axis on which the chart was plotted.*
diet (pd.DataFrame): Diet logging data. Set to None to remove from figure.
cgm (pd.DataFrame): CGM data. Set to None to remove from figure.
sleep_events (pd.DataFrame): Sleep events data. Set to None to remove from figure.
sleep_channels (pd.DataFrame): Sleep channels data. Set to None to remove from figure.
cgm_grid (List[int]): CGM grid lines. Default: [0, 54, 70, 100, 140, 180].
channel_filter (List[str]): Which sleep channels to include in the plot. Default: ['heart_rate', 'actigraph', 'spo2'].
participant_id (int): Participant ID.
array_index (int): Array index.
time_range (Tuple[str, str]): Time range to plot.
figsize (Tuple[int, int]): Figure size.
nutrient_kws (dict): Keyword arguments for diet nutrients lollipop plot.
meals_kws (dict): Keyword arguments for diet meals plot.
cgm_kws (dict): Keyword arguments for CGM plot.
events_kws (dict): Keyword arguments for sleep events plot.
channels_kws (dict): Keyword arguments for sleep channels plot.
Returns:
TimeSeriesFigure: The figure object containing the plots.*
How to plot diet logs
This module provides functions for plotting diet data, as well as a function for plotting diet, CGM and sleep data together.
First, we will load the time series data for diet, CGM and sleep. (See also the dedicated modules for sleep and CGM.)
/home/ec2-user/projects/pheno-utils/pheno_utils/pheno_loader.py:610: UserWarning: No date field found
warnings.warn(f'No date field found')
/home/ec2-user/projects/pheno-utils/pheno_utils/pheno_loader.py:610: UserWarning: No date field found
warnings.warn(f'No date field found')
/home/ec2-user/projects/pheno-utils/pheno_utils/pheno_loader.py:610: UserWarning: No date field found
warnings.warn(f'No date field found')
from pheno_utils.timeseries_plots import TimeSeriesFigureg = TimeSeriesFigure(figsize=(14, 7))time_range = ('2020-06-22 06:00', '2020-06-23 15:00')# Each call to the plot() methods adds a new time-synced subplot to the figureg.plot(plot_nutrient_lollipop, diet_df, size_scale=15, time_range=time_range, name='diet_pie')g.plot(plot_meals_hbars, diet_df, time_range=time_range, name='diet_meals', height=2)g.plot(plot_nutrient_bars, diet_df, time_range=time_range, label=None, n_axes=2, nut_exclude=['sodium'], name='diet_bars')g.set_axis_padding(0.03)