1

Bit of a tricky one to explain. I am trying to create two subplots consisting of a histogram and a boxplot, using data from the same dataframe (df), which has two columns (different lengths) 'price_cml15' and 'price_nw15'. When making these graphs I want to select only a subset of the data from each column (as shown by the minx and maxx variables used to select the data range I want). Ultimately want to do a for loop that goes through various ranges, which works for the histograms at the moment but I can't do the boxplots. The histogram is fine I cannot work out how to get the boxplot to show this specific range for both columns. Using the below code (the sns.boxplot part) I can extract the range from the one column (price_cml15), but can't get it to do this for both columns side by side in the same boxplot, and cannot get it to label this boxplot data correctly. This is the code I have as it stands:

#creates overlapping histograms and boxplot for nw and cml dataframes#

#set minimum and maximum values for x axis#
minx = 0 
maxx = 500000 

#overlaps plots on top of eachother and sets figsize#
fig, ax = plt.subplots(1,2,figsize=(16,4))#,sharey=True, sharex=True)
#set number of bins#
n_bins=100

#selects columns from dataframe(df) to be plotted along with 
characteristics#
ax[0].hist(df['price_cml15'], bins=n_bins, range=(minx,maxx), normed=True, 
edgecolor='red', facecolor='None', linewidth=2, label='cml15')
ax[0].hist(df['price_nw15'], bins=n_bins, range=(minx,maxx), normed=True, 
color='blue', edgecolor='black', alpha = 0.5, linewidth=1, label='nw15')
ax[0].set_yticklabels([])
ax[0].legend()

sns.boxplot(data=(df.loc[(df['price_cml15']>= minx) & (df['price_cml15'] <= 
maxx), 'price_cml15']), ax=ax[1])

#saves fig as .png#
fig.savefig(r'A:\Live system\Monthly 
processing\NW_cml_15_100bins_0to500k.png')

This code creates the following attached subplots, as you can see it works for the one column but it hasn't labelled it properly, and I have tried numerous ways of getting it to do this for both columns on the same boxplot but to no avail. Any help would be much appreciated! Once I have worked this out I think the for loop should be pretty straight forward.

Many thanks

Kat
example of current output

0 Answers0