Marsilea-viz / marsilea

Declarative creation of composable visualization for Python (Complex heatmap, Upset plot, Oncoprint and more~)
https://marsilea.rtfd.io/
MIT License
172 stars 6 forks source link

Problem with violin plot hues when data frames contain NaN #47

Closed jpintar closed 1 month ago

jpintar commented 1 month ago

Problem/New Plotter

Consider a data set with the following structure:

v_data = pd.DataFrame({
    'group': [0, 0, 0, 1, 1, 1, 1, 1, 2, 2],
    'score_A': np.random.randn(1, 10)[0],
    'score_B': np.random.randn(1, 10)[0]
})

To make a violin plot of this data set in Seaborn, we can melt the data-frame:

v_data_melted = v_data.melt(
    id_vars='group',
    value_vars=['score_A', 'score_B'],
    var_name='score_type',
    value_name='score'
)

and use the resulting long-format frame like so:

sns.violinplot(data=v_data_melted, x='group', y='score', hue='score_type')

I was trying to add such a violin plot to a Marsilea heatmap, but since ma.plotter.Violin only accepts wide-format data, my approach was the following:

h_data = np.random.randn(3, 10)

v_data_A = v_data[['group', 'score_A']].pivot(columns='group', values='score_A')
v_data_B = v_data[['group', 'score_B']].pivot(columns='group', values='score_B')

m = ma.Heatmap(h_data)
v = ma.plotter.Violin({'A': v_data_A, 'B': v_data_B})
m.add_right(v)
m.render()

However, that fails with a ValueError: cannot reindex on an axis with duplicate labels.

This error seems to be related to all the NaNs in the data-frames. The following renders without error, but is the wrong result, naturally:

m = ma.Heatmap(h_data)
v = ma.plotter.Violin({'A': v_data_A.fillna(0), 'B': v_data_B.fillna(0)})
m.add_right(v)
m.render()

Is this a bug, or am I just doing something wrong?

Proposed solution

No response

jpintar commented 1 month ago

Solved by updating to Marsilea 0.4.6.

Mr-Milk commented 1 month ago

Thanks for using Marsilea, this is fixed since 4.5.0. If you encounter any other issues or you don't know how to make a visualization with Marsilea, feel free to open a new issue, I'm happy to help.