0

I would like to create a tornado plot using seaborn objects and was wondering if there was an efficient way to do this.

import numpy as np
import pandas as pd
import seaborn.objects as so

# create a dataframe with the columns id, gender, and age with random values
df = pd.DataFrame({'id':np.random.randint(0, 1000, 100), "gender":np.random.choice(['m', 'f'], 100), "age":np.random.randint(18, 100, 100)})
so.Plot(data=df, x="age", y='id', color='gender').add(so.Bars(), so.Hist(), so.Stack()) 

This code generates a stacked histogram that looks similar to this:

enter image description here

Now rather than having the bars stacked I would like to have one color on the positive axis and the other on the negative axis. Is there a way to get there without having to create two plots and gluing them together?

I understand that maybe there is not, because the solution would not be generic to any number of different items in the gender column, but then again, maybe there is.

3
  • df.loc[df.gender.eq('m'), 'id'] = df.loc[df.gender.eq('m'), 'id'].mul(-1) and so.Plot(data=df, x="age", y='id', color='gender').add(so.Bars()) Commented Apr 13, 2023 at 16:51
  • df['age range'] = pd.cut(df.age, bins=range(10, 110, 10)), df.loc[df.gender.eq('m'), 'id'] = df.loc[df.gender.eq('m'), 'id'].mul(-1), dfg = df.groupby(['age range', 'gender'])['id'].sum().reset_index(), so.Plot(data=dfg, x="age range", y='id', color='gender').add(so.Bars()) Commented Apr 13, 2023 at 17:10
  • 1
    Thanks a lot, but I believe you are plotting the actual IDs, I would need the histogram, so count of IDs. But this already points me in the right direction. Commented Apr 14, 2023 at 9:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.