Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #71852

Adding R squared value to scatter plot

Newsgroups comp.lang.python
Date 2014-05-21 04:59 -0700
Message-ID <47d85028-12c7-4e5b-b540-33a12539fbd2@googlegroups.com> (permalink)
Subject Adding R squared value to scatter plot
From Jamie Mitchell <jamiemitchell1604@gmail.com>

Show all headers | View raw


I have made a plot using the following code:

python2.7
import netCDF4
import matplotlib.pyplot as plt
import numpy as np

swh_Q0_con_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/controlperiod/south_west/swhcontrol_swest_annavg1D.nc','r')
hs_Q0_con_sw=swh_Q0_con_sw.variables['hs'][:]
swh_Q3_con_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q3/swh/controlperiod/south_west/swhcontrol_swest_annavg1D.nc','r')
hs_Q3_con_sw=swh_Q3_con_sw.variables['hs'][:]
swh_Q4_con_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q4/swh/controlperiod/south_west/swhcontrol_swest_annavg1D.nc','r')
hs_Q4_con_sw=swh_Q4_con_sw.variables['hs'][:]
swh_Q14_con_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q14/swh/controlperiod/south_west/swhcontrol_swest_annavg1D.nc','r')
hs_Q14_con_sw=swh_Q14_con_sw.variables['hs'][:]
swh_Q16_con_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q16/swh/controlperiod/south_west/swhcontrol_swest_annavg1D.nc','r')
hs_Q16_con_sw=swh_Q16_con_sw.variables['hs'][:]
swh_Q0_fut_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/2050s/south_west/swh2050s_swest_annavg1D.nc','r')
hs_Q0_fut_sw=swh_Q0_fut_sw.variables['hs'][:]
swh_Q3_fut_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q3/swh/2050s/south_west/swh2050s_swest_annavg1D.nc','r')
hs_Q3_fut_sw=swh_Q3_fut_sw.variables['hs'][:]
swh_Q4_fut_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q4/swh/2050s/south_west/swh2050s_swest_annavg1D.nc','r')
hs_Q4_fut_sw=swh_Q4_fut_sw.variables['hs'][:]
swh_Q14_fut_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q14/swh/2050s/south_west/swh2050s_swest_annavg1D.nc','r')
hs_Q14_fut_sw=swh_Q14_fut_sw.variables['hs'][:]
swh_Q16_fut_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q16/swh/2050s/south_west/swh2050s_swest_annavg1D.nc','r')
hs_Q16_fut_sw=swh_Q16_fut_sw.variables['hs'][:]

fit_Q0_sw=np.polyfit(hs_Q0_con_sw,hs_Q0_fut_sw,1)
fit_fn_Q0_sw=np.poly1d(fit_Q0_sw)

plt.plot(hs_Q0_con_sw,hs_Q0_fut_sw,'g.')
plt.plot(hs_Q0_con_sw,fit_fn_Q0_sw(hs_Q0_con_sw),'g',label='Q0 no pert')

fit_Q3_sw=np.polyfit(hs_Q3_con_sw,hs_Q3_fut_sw,1)
fit_fn_Q3_sw=np.poly1d(fit_Q3_sw)

plt.plot(hs_Q3_con_sw,hs_Q3_fut_sw,'b.')
plt.plot(hs_Q3_con_sw,fit_fn_Q3_sw(hs_Q3_con_sw),'b',label='Q3 low sens')

fit_Q4_sw=np.polyfit(hs_Q4_con_sw,hs_Q4_fut_sw,1)
fit_fn_Q4_sw=np.poly1d(fit_Q4_sw)

plt.plot(hs_Q4_con_sw,hs_Q4_fut_sw,'y.')
plt.plot(hs_Q4_con_sw,fit_fn_Q4_sw(hs_Q4_con_sw),'y',label='Q4 low sens')

fit_Q14_sw=np.polyfit(hs_Q14_con_sw,hs_Q14_fut_sw,1)
fit_fn_Q14_sw=np.poly1d(fit_Q14_sw)

plt.plot(hs_Q14_con_sw,hs_Q14_fut_sw,'r.')
plt.plot(hs_Q14_con_sw,fit_fn_Q14_sw(hs_Q14_con_sw),'r',label='Q14 high sens')

fit_Q16_sw=np.polyfit(hs_Q16_con_sw,hs_Q16_fut_sw,1)
fit_fn_Q16_sw=np.poly1d(fit_Q16_sw)

plt.plot(hs_Q16_con_sw,hs_Q16_fut_sw,'c.')
plt.plot(hs_Q16_con_sw,fit_fn_Q16_sw(hs_Q16_con_sw),'c',label='Q16 high sens')

plt.legend(loc='best')
plt.xlabel('Significant Wave Height annual averages NW Scotland 1981-2010')
plt.ylabel('Significant Wave Height annual averages NW Scotland 2040-2069')
plt.title('Scatter plot of Significant Wave Height')
plt.show()

--

What I would like to do is display the R squared value next to the line of best fits that I have made. 

Does anyone know how to do this with matplotlib?

Thanks,

Jamie

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Adding R squared value to scatter plot Jamie Mitchell <jamiemitchell1604@gmail.com> - 2014-05-21 04:59 -0700
  Re: Adding R squared value to scatter plot Jason Swails <jason.swails@gmail.com> - 2014-05-21 08:30 -0400
    Re: Adding R squared value to scatter plot Jamie Mitchell <jamiemitchell1604@gmail.com> - 2014-06-05 08:55 -0700

csiph-web