0

I have a dataframe with 2 arrays that I want to compare and year (2017, 2018, 2019, 2020, 2021)

        d_value                      d_price    year
    -0.5064707042427661 0.023998098076044583    2017
    0.28441876232859986 0.0022609116336060886   2017
    0.5367803487234388  0.13772218694253624     2017
    0.0377291747906765  0.021950729043333617    2017
    -0.18032315533671328    -0.04870435126455952    2017
    -0.36067080655671513    -0.019264936817780298   2017
    -0.372303636796094  0.01799282703076832     2017

   ...

    0.007174422512419509    0.0019633270749088716   2019
   -0.12315438362311693   -0.001272120509836161     2019
    0.06807880349789097 -0.027139767879056143       2019
    -0.0415780440397856 0.0005471861484347418       2019
    -0.07717256578262432    -0.011382707712158657   2019
    0.2627255062420586  -0.0021274426812466496      2019
    -0.13879042170946043    0.009918696052991338    2019
    -0.040799674958042265   -0.0044788215666135     2019
....

I used this to plot all years in one graph

sns.set(rc={'figure.figsize':(6,5)})
plt = sns.scatterplot(data=corr, x="d_price", y="d_value", hue="year")
#plt.title('Price-Volume Correlation ', fontsize=18)
plt.set_xlabel("Change in price", fontsize = 10)
plt.set_ylabel("Change in total transaction volume (USD)", fontsize = 10)

enter image description here

but there are just too many data points, so I want to have separate plots by year, 5 scatter plots in one grid. Is there a way to do that instead of manually producing 5 separate plots? and also, is there a way to insert a text in each plot something like "corr: 0.22"? Thanks a lot in advance!

Olive
  • 644
  • 4
  • 12
  • 1
    You might want to look into [sns.relplot](https://seaborn.pydata.org/generated/seaborn.relplot.html). E.g. `sns.relplot(data=corr, x="d_price", y="d_value", col="year", col_wrap=3, kind='scatter')` – JohanC Mar 21 '22 at 21:29
  • This is actually nice, but i want to use sns.regplot, but this doesn't allow col_wrap, and col="year" options. anyidea how to do this on a regplot? – Olive Mar 21 '22 at 22:12
  • [`sns.lmplot`](https://seaborn.pydata.org/generated/seaborn.lmplot.html#seaborn.lmplot) is the "figure-level" extension of `sns.regplot`. – JohanC Mar 21 '22 at 22:31
  • See the second answer at [Seaborn implot with equation and R2 text](https://stackoverflow.com/questions/25579227/seaborn-implot-with-equation-and-r2-text) about adding annotations. – JohanC Mar 21 '22 at 22:43
  • Johan, that's useful thanks, I used "g = sns.lmplot(data=corr, x="d_price", y="d_value", col="year", ci=68, col_wrap=3, scatter_kws={'linewidths':0.5,'edgecolor':'w'}) " and i was able to get the regression line, I also wanted the slope coefficients so i tried copying the second code to mine but i get stuck at "plt.gca()", it somehow gives me an error msg: AttributeError: 'AxesSubplot' object has no attribute 'gca'. Do you know why? – Olive Mar 21 '22 at 23:21
  • 1
    In your code, there is a line `plt = sns.scatterplot(...)`. This overwrites the `plt` variable (created via `import matplotlib.pyplot as plt`). You could change that bad variable name, and restart your environment to be able again to access `plt.gca()`. – JohanC Mar 21 '22 at 23:41
  • Ok, i changed but still doesn't work.. keep getting "AttributeError: 'AxesSubplot' object has no attribute 'gca' – Olive Mar 22 '22 at 11:42
  • If you change but don't restart, the badly named variable will still live in your environment. – JohanC Mar 22 '22 at 12:23

0 Answers0