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

List of colors not allowed when hist = True #73

Open
mrariden opened this issue Apr 24, 2024 · 0 comments
Open

List of colors not allowed when hist = True #73

mrariden opened this issue Apr 24, 2024 · 0 comments

Comments

@mrariden
Copy link

The following code works and plots the densities for each of my Files.

colors = ['red', 'red', 'red', 'grey', 'grey', 'grey', 'green', 'green', 'green', 'blue', 'blue', 'blue']
fig, axes = joypy.joyplot(img_data_df, 
                          by='File',
                          column='MinFeret',
                          color=colors,
                          bins=50,
                          overlap=0,
                         )

but when I plot the histograms:

fig, axes = joypy.joyplot(img_data_df, 
                          by='File',
                          column='MinFeret',
                          color=colors,
                          bins=50,
                          overlap=0,
                          hist=True,      # Produces error
                         )

matplotlib.hist() expects color to only be a single value. Which throws an error here:

joypy/joypy/joyplot.py

Lines 465 to 469 in fb74cb6

if hist:
# matplotlib hist() already handles multiple subgroups in a histogram
a.hist(group, label=sublabels, bins=bins, color=color,
range=[min(global_x_range), max(global_x_range)],
edgecolor=linecolor, zorder=group_zorder, **kwargs)



Traceback

ValueError Traceback (most recent call last)
Cell In[11], line 10
7 f_group = get_group(f)
8 colors.append(palette[f_group])
---> 10 fig, axes = joypy.joyplot(img_data_df,
11 by='File',
12 column='MinFeret',
13 color=colors,
14 hist=True,
15 bins=50,
16 overlap=0,
17 )

File ~/school/ivan_img_reanalysis/venv/lib/python3.10/site-packages/joypy/joyplot.py:241, in joyplot(data, column, by, grid, xlabelsize, xrot, ylabelsize, yrot, ax, figsize, hist, bins, fade, ylim, fill, linecolor, overlap, background, labels, xlabels, ylabels, range_style, x_range, title, colormap, color, normalize, floc, **kwds)
237 if any(len(subg)==0 for g in converted for subg in g):
238 warn("At least a column/group has no numeric values.")
--> 241 return _joyplot(converted, labels=labels, sublabels=sublabels,
242 grid=grid,
243 xlabelsize=xlabelsize, xrot=xrot, ylabelsize=ylabelsize, yrot=yrot,
244 ax=ax, figsize=figsize,
245 hist=hist, bins=bins,
246 fade=fade, ylim=ylim,
247 fill=fill, linecolor=linecolor,
248 overlap=overlap, background=background,
249 xlabels=xlabels,
250 range_style=range_style, x_range=x_range,
251 title=title,
252 colormap=colormap,
253 color=color,
254 normalize=normalize,
255 floc=floc,
256 **kwds)

File ~/school/ivan_img_reanalysis/venv/lib/python3.10/site-packages/joypy/joyplot.py:467, in _joyplot(data, grid, labels, sublabels, xlabels, xlabelsize, xrot, ylabelsize, yrot, ax, figsize, hist, bins, fade, xlim, ylim, fill, linecolor, overlap, background, range_style, x_range, tails, title, legend, loc, colormap, color, normalize, floc, **kwargs)
463 num_subgroups = len(group)
465 if hist:
466 # matplotlib hist() already handles multiple subgroups in a histogram
--> 467 a.hist(group, label=sublabels, bins=bins, color=color,
468 range=[min(global_x_range), max(global_x_range)],
469 edgecolor=linecolor, zorder=group_zorder, **kwargs)
470 else:
471 for j, subgroup in enumerate(group):
472
473 # Compute the x_range of the current plot

File ~/school/ivan_img_reanalysis/venv/lib/python3.10/site-packages/matplotlib/init.py:1465, in _preprocess_data..inner(ax, data, *args, **kwargs)
1462 @functools.wraps(func)
1463 def inner(ax, *args, data=None, **kwargs):
1464 if data is None:
-> 1465 return func(ax, *map(sanitize_sequence, args), **kwargs)
1467 bound = new_sig.bind(ax, *args, **kwargs)
1468 auto_label = (bound.arguments.get(label_namer)
1469 or bound.kwargs.get(label_namer))

File ~/school/ivan_img_reanalysis/venv/lib/python3.10/site-packages/matplotlib/axes/_axes.py:6818, in Axes.hist(self, x, bins, range, density, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)
6816 colors = mcolors.to_rgba_array(color)
6817 if len(colors) != nx:
-> 6818 raise ValueError(f"The 'color' keyword argument must have one "
6819 f"color per dataset, but {nx} datasets and "
6820 f"{len(colors)} colors were provided")
6822 hist_kwargs = dict()
6824 # if the bin_range is not given, compute without nan numpy
6825 # does not do this for us when guessing the range (but will
6826 # happily ignore nans when computing the histogram).

ValueError: The 'color' keyword argument must have one color per dataset, but 1 datasets and 12 colors were provided



I fixed it for myself by iterating through the subgroups and plotting the histograms separately:
github.com/mrariden/joypy/blob/master/joypy/README.md?plain=1#L14

I can make a PR if this is a reasonable solution.

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