Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

visualize based on two pandas data frame columns (continuous values and marker style)

I would like to visualize a dataset (with legend and color bar). The dataset is a pandas dataframe as follows: X and Y are the values, the year column defines the year of data collection, and the shape column defines the method that is used in data collection. I am wondering how I can plot Y against X while color-coded based on years and markers (and therefore legend) defined by shape in one command such as plot.

df = pd.DataFrame({
    'X':     [1,    1,    2,    2,    2,    3,    3,    4],
    'Y':     [3,    4,    1,    6,    7,    8,    8,    5],
    'year':  [1998, 1999, 1994, 1991, 1999, 1995, 1994, 1992],
    'shape': ['o',  '^',  'o',  '^',  'o',  '^',  'o',  '^']
})

Of course, I can loop over the shapes and plot per shape (method) and add a color bar separately as well ensuring the min and max of the years in the color bar and legend based on shape. I would like to avoid loop and manual setting of legend or color bar if possible. I am wondering if there is a better way forward. Thank you!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This sounds like a job for seaborn:

import seaborn as sns
sns.scatterplot(data=df, x='X', y='Y',
                hue='year',
                style='shape',
                markers=df['shape'].unique().tolist()
)

Output:

enter image description here

Is this what you are looking for?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading