Igor Igor
Igor Igor

Reputation: 315

Two dataframe boxplots on one graph python

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?

Upvotes: 2

Views: 1527

Answers (1)

StupidWolf
StupidWolf

Reputation: 46908

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

Upvotes: 1

Related Questions