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?

Igor Igor
  • 305
  • 1
  • 7
  • Does this answer your question? Consider upvoting if it does. https://stackoverflow.com/questions/63512119/plot-sequential-box-plots-in-matplotlib-control-and-treatment-groups/63515102#63515102 – Arsik36 Oct 10 '20 at 18:36

1 Answers1

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

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • 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. – Igor Igor Oct 10 '20 at 19:26
  • It's not so straight forward with pd.boxplot.. see https://stackoverflow.com/questions/50963960/change-color-of-individual-boxes-in-pandas-boxplot-subplots/50967405 – StupidWolf Oct 10 '20 at 19:51
  • 1
    you can use seaborn ; ```sns.boxplot(data=pd.concat([df,df1]),y='price',x='urban',hue='urban')``` – StupidWolf Oct 10 '20 at 19:52