Issue
I have a dataset in a spreadsheet in two columns something similar to below:
type [1, 0, 1, 1, 0, 0, 0, 1, 0, 0]
value [230, 300, 342, 218, 393, 273, 333, 317, 287, 291]
I want to group the values of 0 types and 1 types and boxplot the three data sets (original set, 0s and 1s) in a single frame.
I have tried a few different things but none has worked:
import matplotlib.pyplot as plt
import numpy
import pandas a pd
inData = pd.read_csv(sheet)
x = inData['value']
grouped = inData.groupby(["type"])
out0, out1 = [grouped.get_group(value) for value in grouped.groups]
fig1, ax1 = plt.subplots()
ax1.set_title('Box Plot')
data = [out0, value, out1[::2]]
ax1.boxplot(data)
plt.show()
Boxplot has to be constructed using python/matplotlibs.
Solution
You can concat
the dataset with itself while assigning a combined label, then use seaborn.boxplot
:
import seaborn as sns
df = pd.DataFrame({'type': [1, 0, 1, 1, 0, 0, 0, 1, 0, 0],
'value': [230, 300, 342, 218, 393, 273, 333, 317, 287, 291]
})
sns.boxplot(data=pd.concat([df, df.assign(type='both')]),
x='type', y='value', order=['both', 0, 1]
)
output:
pure matplotlib solution
df = pd.DataFrame({'type': [1, 0, 1, 1, 0, 0, 0, 1, 0, 0],
'value': [230, 300, 342, 218, 393, 273, 333, 317, 287, 291]
})
df2 = pd.concat([df, df.assign(type='both')]).groupby('type')['value'].apply(list)
ax = plt.subplot()
ax.boxplot(df2, labels=df2.index)
output:
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.