0

I have a dataframe containing returns for 4 different tickers, and I'm trying to plot returns by days of the week in seaborn.

The code I have is:

fig, ax = plt.subplots(1,4, figsize = (30, 10))
plt.subplots_adjust(hspace = .5)
fig.suptitle("Daily returns by ticker\nJan 2020 - April 15, 2022", fontsize = 18)

list_of_tickers = ['VIX','UVXY','VXX','GSPC']


for ticker, axs in zip(list_of_tickers, ax.ravel()):
    
    dfs = master_df[master_df['ticker'] == ticker]
    ax = sns.catplot(data = dfs, x = 'returns', y = 'day', kind = 'box', showfliers = False, ax = axs)
    plt.title("Daily returns for " + ticker + " since 2020")
    plt.axvline(0, color = 'red')
    sns.set_style('dark')

plt.show()

Which is outputting: Outout image

Instead of filling the subplots.

I assume this has something to do with using catplot instead of boxplot, but I don't know of another way to structure my dataframe to get these results outside of making day categorical.

I have included a sample dataframe; Any help or advice is greatly appreciated.

    ticker  day       returns
0   VIX     Thursday    
1   VIX     Friday    20.37
2   VIX     Monday    10.2
3   VIX     Tuesday   -0.07
4   VIX     Wednesday  9.93
5   VIX     Thursday  -3.72
6   VIX     Friday    -0.96
7   VIX     Monday     2.23
8   VIX     Tuesday    3.25
9   VIX     Wednesday  3.23
10  UVXY    Thursday   2.45 
11  UVXY    Friday    11.71
12  UVXY    Monday     4.92
13  UVXY    Tuesday    0.7
14  UVXY    Wednesday -1.41
15  UVXY    Thursday  -4.04
16  UVXY    Friday    -0.43
17  UVXY    Monday    -1.64
18  UVXY    Tuesday    0.27
19  UVXY    Wednesday -0.18
20  VXX     Thursday   3.44
21  VXX     Friday     7.99
22  VXX     Monday     2.94
23  VXX     Tuesday    0.53
24  VXX     Wednesday -0.67
25  VXX     Thursday  -2.84
26  VXX     Friday    -0.14
27  VXX     Monday    -1.13
28  VXX     Tuesday    0.22
29  VXX     Wednesday  0.0
30  GSPC    Thursday  -1.3
31  GSPC    Friday    -0.97
32  GSPC    Monday    -0.53
33  GSPC    Tuesday   -0.14
34  GSPC    Wednesday  0.04
35  GSPC    Thursday   0.4
36  GSPC    Friday     0.22
37  GSPC    Monday     0.18
38  GSPC    Tuesday   -0.08
39  GSPC    Wednesday -0.03
birdman
  • 125
  • 7
  • You need `sns.boxplot(...., ax=axs)` instead of `sns.catplot(...)`. `sns.catplot` is a "figure-level" function that always creates its own figure with one or more subplots. `sns.boxplot` is an axes-level function which can be plotted on a given subplot. See [Seaborn's function overview](https://seaborn.pydata.org/tutorial/function_overview.html) – JohanC Apr 20 '22 at 00:14
  • Huh, amazing. I tried that earlier and was receiving error messages; thank you! – birdman Apr 20 '22 at 00:16

0 Answers0