Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

month_grid=True fails when a month has no valid data; specify a separate colour for missing values like np.nan #42

Open
FeiYao-Edinburgh opened this issue Feb 13, 2025 · 1 comment

Comments

@FeiYao-Edinburgh
Copy link

Hi there,

import numpy as np
import matplotlib.pyplot as plt
import july
from july.utils import date_range

dates = date_range("2020-01-01", "2020-12-31")
data = np.random.rand(len(dates))

data[0:50] = np.nan
data[300:] = np.nan

# we'll at least need one data for each month to enable month_grid...
july.heatmap(
    dates,
    data,
    title='Github Activity',
    cmap=cmaps.WhiteBlueGreenYellowRed,
    month_grid=True,)

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[66], line 13
     10 data[300:] = np.nan
     12 # we'll at least need one data for each month to enable month_grid...
---> 13 july.heatmap(
     14     dates,
     15     data,
     16     title='Github Activity',
     17     cmap=cmaps.WhiteBlueGreenYellowRed,
     18     month_grid=True,)

File ~/scratch/miniconda3/envs/gcadj/lib/python3.10/site-packages/july/plot.py:75, in heatmap(dates, data, horizontal, cmap, value_label, date_label, weekday_label, month_label, year_label, month_grid, month_grid_color, colorbar, frame_on, value_format, title, cmin, cmax, cbar_label_format, ax, **kwargs)
     73 dates_clean, data_clean = preprocess_inputs(dates, data)
     74 cal = date_grid(dates_clean, data_clean, horizontal)
---> 75 ax = cal_heatmap(
     76     cal=cal,
     77     dates=dates_clean,
     78     horizontal=horizontal,
     79     cmap=cmap,
     80     value_label=value_label,
     81     date_label=date_label,
     82     weekday_label=weekday_label,
     83     month_label=month_label,
     84     year_label=year_label,
     85     month_grid=month_grid,
     86     month_grid_color=month_grid_color,
     87     colorbar=colorbar,
     88     frame_on=frame_on,
     89     value_format=value_format,
     90     title=title,
     91     cmin=cmin,
     92     cmax=cmax,
     93     cbar_label_format=cbar_label_format,
     94     ax=ax,
     95 )
     97 return ax

File ~/scratch/miniconda3/envs/gcadj/lib/python3.10/site-packages/july/helpers.py:96, in cal_heatmap(cal, dates, horizontal, cmap, value_label, date_label, weekday_label, month_label, year_label, month_grid, month_grid_color, colorbar, frame_on, value_format, title, cmin, cmax, cbar_label_format, ax)
     94     add_year_label(ax, dates, horizontal)
     95 if month_grid:
---> 96     add_month_grid(ax, dates, cal, horizontal, month_grid_color)
     97 if colorbar:
     98     add_colorbar(pc, fig, ax, bbox, cbar_label_format)

File ~/scratch/miniconda3/envs/gcadj/lib/python3.10/site-packages/july/helpers.py:280, in add_month_grid(ax, dates, month_grid, horizontal, color)
    278 months = set([d.month for d in dates])
    279 for month in months:
--> 280     coords = get_month_outline(
    281         dates, month_grid, horizontal=horizontal, month=month
    282     )
    283     ax.plot(coords[:, 0], coords[:, 1], color=color, linewidth=1)
    285 # Pad axes so plotted line appears uniform also along edges.

File ~/scratch/miniconda3/envs/gcadj/lib/python3.10/site-packages/july/helpers.py:243, in get_month_outline(dates, month_grid, horizontal, month)
    240                 coords_list.append((x, y))
    242 sorted_coords = np.array(coords_list)
--> 243 min_y = sorted_coords[:, 1].min()
    244 max_y = sorted_coords[:, 1].max()
    245 upper_left = sorted_coords[0]

IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed
@FeiYao-Edinburgh
Copy link
Author

As related, I believe it would be fantastic if we could specify a separate colour for missing values like np.nan, instead of filling them with zero, as currently done in utils.preprocess_inputs(). Could the community assist with this?

@FeiYao-Edinburgh FeiYao-Edinburgh changed the title month_grid is not working when there are no valid data in a month month_grid is not working when there are no valid data in a month; specify a separate colour for missing values like np.nan Feb 14, 2025
@FeiYao-Edinburgh FeiYao-Edinburgh changed the title month_grid is not working when there are no valid data in a month; specify a separate colour for missing values like np.nan month_grid=True fails when a month has no valid data; specify a separate colour for missing values like np.nan Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant