Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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')
like image 482
Svenno Nito Avatar asked May 30 '26 01:05

Svenno Nito


1 Answers

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

like image 55
ImportanceOfBeingErnest Avatar answered May 31 '26 13:05

ImportanceOfBeingErnest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!