prince icon indicating copy to clipboard operation
prince copied to clipboard

How to better view plots when points are close to eachother

Open mas192 opened this issue 4 years ago • 2 comments

Hi Everyone, I am glad that this package is still maintained and on going, I need advice on how to better visualize through ca.plot_coordinates() Right now, there is a massive overlap of component on the 2d plot and its very hard to understand the relationship. Is there an equivalent of jitter or some offset so that its easy to see two points in close proximity. Please let me know if my question makes sense. Thanks Sam

mas192 avatar Jun 02 '21 17:06 mas192

I had the same problem with a plot of outputs from MCA. I wrote the following class to inherit from prince and plot using plotly so I could zoom in and out. I slimmed it down a lot though because I was only interested in the columns.

import prince
import plotly.graph_objects as pgo

class plotlyMCA(prince.MCA):
        
    def plot_coordinates_plotly(self, X, x_component=0, y_component=1, show_column_points=True):

        fig = pgo.Figure()

        # Plot column principal coordinates
        if show_column_points or show_column_labels:

            col_coords = self.column_coordinates(X)
            x = col_coords[x_component]
            y = col_coords[y_component]

            prefixes = col_coords.index.str.split('_').map(lambda x: x[0])

            fig.add_traces([pgo.Scatter(
                x=x[prefixes == prefix], 
                y=y[prefixes == prefix], 
                hovertext=col_coords[prefixes == prefix].index, 
                mode='markers') for prefix in prefixes.unique()])

        # Text
        fig.update_layout(title='Row and column principal coordinates', showlegend=False)
        ei = self.explained_inertia_
        fig.update_xaxes(title_text='Component {} ({:.2f}% inertia)'.format(x_component, 100 * ei[x_component]))
        fig.update_yaxes(title_text='Component {} ({:.2f}% inertia)'.format(y_component, 100 * ei[y_component]))

        return fig

mdhanna avatar Jun 07 '21 20:06 mdhanna

Hi mdhannaff,

Thanks so much for this solution I will try this for now. I really appreciate your help! Best Regards Sam Joshva. B

On Mon, 7 Jun 2021 at 13:03, mdhanna @.***> wrote:

I had the same problem with a plot of outputs from MCA. I wrote the following class to inherit from prince and plot using plotly so I could zoom in and out. I slimmed it down a lot though because I was only interested in the columns.

import prince import plotly.graph_objects as pgo

class plotlyMCA(prince.MCA):

def plot_coordinates_plotly(self, X, x_component=0, y_component=1, show_column_points=True):

    fig = pgo.Figure()

    # Plot column principal coordinates
    if show_column_points or show_column_labels:

        col_coords = self.column_coordinates(X)
        x = col_coords[x_component]
        y = col_coords[y_component]

        prefixes = col_coords.index.str.split('_').map(lambda x: x[0])

        fig.add_traces([pgo.Scatter(
            x=x[prefixes == prefix],
            y=y[prefixes == prefix],
            hovertext=col_coords[prefixes == prefix].index,
            mode='markers') for prefix in prefixes.unique()])

    # Text
    fig.update_layout(title='Row and column principal coordinates', showlegend=False)
    ei = self.explained_inertia_
    fig.update_xaxes(title_text='Component {} ({:.2f}% inertia)'.format(x_component, 100 * ei[x_component]))
    fig.update_yaxes(title_text='Component {} ({:.2f}% inertia)'.format(y_component, 100 * ei[y_component]))

    return fig

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/MaxHalford/prince/issues/114#issuecomment-856217491, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFCDRXTD7KCV7WDEEA5WRVTTRUQ2DANCNFSM457GI32Q .

mas192 avatar Jun 08 '21 04:06 mas192

Hello there 👋

I apologise for not answering earlier. I was not maintaining Prince anymore. However, I have just refactored the entire codebase. This refactoring should have fixed many bugs.

I don’t have time and energy to check if this fixes your issue, but there is a good chance it does. Feel free to reopen this issue if the problem persists after installing the new version — that is, version 0.8.0 and onwards.

MaxHalford avatar Feb 27 '23 11:02 MaxHalford

Thanks MaxHalford/prince 🙏

On Mon, Feb 27, 2023, 03:46 Max Halford @.***> wrote:

Closed #114 https://github.com/MaxHalford/prince/issues/114 as completed.

— Reply to this email directly, view it on GitHub https://github.com/MaxHalford/prince/issues/114#event-8614809222, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFCDRXW7A7PQS6LY4ZWV3ELWZSHYZANCNFSM457GI32Q . You are receiving this because you authored the thread.Message ID: @.***>

mas192 avatar Feb 27 '23 16:02 mas192