2

I'm trying to construct two boxplots on one graph in python:

ax = df.boxplot(column = ['price'], by = ['urban'],meanline=True, showmeans=True, showcaps=True, 
            showbox=True, showfliers=False, return_type='axes')
df1.boxplot(column = ['price'], by = ['urban'], meanline=True, showmeans=True, showcaps=True, 
           showbox=True, showfliers=False, ax=ax)

These boxplots are built on the same graph, but they overlap each other. Whereas in df 'urban' is always equal to 1 (and this works correctly on a separate boxplot) and in df1 it is always 0. On the general graph they both are shown as 0. How can I fix it?

1

1 Answer 1

1

You can concatenate the data.frames and plot:

df = pd.DataFrame({'price':np.random.uniform(0,100,100), 'urban':np.repeat(0,100)})
df1 = pd.DataFrame({'price':np.random.uniform(0,100,100), 'urban':np.repeat(1,100)})

pd.concat([df,df1]).boxplot(column='price',by='urban')

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! It is working really nice, but can I somehow change the color of these boxplots separately? I have tried a list with colors but nothing works.
It's not so straight forward with pd.boxplot.. see stackoverflow.com/questions/50963960/…
you can use seaborn ; sns.boxplot(data=pd.concat([df,df1]),y='price',x='urban',hue='urban')

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.