Svenno Nito Svenno Nito - 3 years ago 214
Python Question

Python: plot panda dataframe with groupby - weird output

I have a dataframe (of type object, for reasons of inserting lists in certain steps beforehand) with three columns looking like this

data stimulus trial
0 2 -2 1
1 2 -2 2
2 2 -2 3
3 2 -2 4
4 2 -2 5
5 2 -2 6
6 1 -2 7
...
159 1 2.5 16


Currently I'm using seaborn but I'm having difficulties inserting proper legends to my plot.

# spi_num is my dataframe
sns.swarmplot(x="stimulus", y="data", data=spi_num.astype(np.float), edgecolor="black", linewidth=.9)
sns.boxplot(x="stimulus", y="data", data=spi_num.astype(np.float), saturation=1)




So I have two questions. How do I smoothly integrate a legend with seaborn? And how would I get this plot using pandas plot commands? I thought I would need something like this:

spi_num.astype(np.float).groupby('stimulus').plot.box()


But then I get 10 figures (one for each stimulus) with 3 boxplots for each xlabel i.e. "data", "stimulus" and "trial". Shouldn't this give me a plot like shown above? At least he does it like this .

enter image description here




Build my dataframe



trial_vec = np.tile(np.arange(16)+1, 10)
stimulus_vec = np.repeat([-2., -1.75, -1., -0.75, -0.5, 0.5, 1., 1.25, 1.75, 2.5 ], 16)
data_vec = np.random.randint(0, 16, size=160)
spi_num = pd.DataFrame({'trial': trial_vec, 'stimulus': stimulus_vec, 'data': data_vec}).astype('object')

Answer Source

You may use DataFrame.boxplot to get the desired box plot

spi_num.astype(np.float).boxplot(column="data", by="stimulus")

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download