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

Plot dataframe using markers based on another dataframe

I am trying to plot df2 as a regular continuous plot but using the values from df1 to select markers.

DATAdict = {
    'A': [ "foo",         "miau",  "ciao" ],
    'B': [ "miau",         "haha",  "ciao"],
    }

df1 = pd.DataFrame.from_dict(DATAdict, orient='columns')

DATAdict = {
    'A': [ 0.79, 0.86, 0.88, ],
    'B': [ 0.89, 0.89, 0.90, ],
    }

df2 = pd.DataFrame.from_dict(DATAdict, orient='columns')

unique = pd.unique(df1.values.ravel('K'))

markers = {}
marker_list = ["s", "o", "+", "x", "P"]
for label, i in zip(unique, range(0, len(unique))):
    markers[label] = marker_list[i]

The idea is to plot A column using range(0,3) for the x axis and 0.79, 0.86, etc for the y axis and then select the markers based on foo, miau and ciao.

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 :

The simplest way is to use sns.scatterplot with the style param:

style : vector or key in data

Grouping variable that will produce points with different markers. Can have a numeric dtype but will always be treated as categorical.

To connect the lines, I don’t know of a way to do that with sns.scatterplot, but we can overlay the line using the Axes handle:

import seaborn as sns
ax = sns.scatterplot(data=df2.reset_index(), x='index', y='A', style=df1['A'], s=100)
ax.plot(df2.index, df2['A'])

Note that seaborn chooses the style markers automatically. You can use the markers param to set them manually, but the markers cannot mix filled and unfilled markers:

marker_list = ['s', 'o', 'P', 'X'] # all "filled" type
ax = sns.scatterplot(data=df2.reset_index(), x='index', y='A', style=df1['A'], markers=marker_list, s=100)

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