Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '21,': 0.07; 'made.': 0.07; 'skip:p 60': 0.07; 'annotations': 0.09; 'canvas.': 0.09; 'matplotlib': 0.09; 'subject:plot': 0.09; 'api': 0.11; 'cc:addr :python-list': 0.11; 'appear.': 0.16; 'cc:name:python list': 0.16; 'compute': 0.16; 'fits': 0.16; 'numpy': 0.16; 'programmer,': 0.16; 'squared': 0.16; 'subject:Adding': 0.16; 'flexibility': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'skip:f 30': 0.19; 'skip:p 40': 0.19; 'import': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'helper': 0.24; 'cc:2**0': 0.24; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'specifically': 0.29; 'am,': 0.29; 'label': 0.30; 'message- id:@mail.gmail.com': 0.30; 'python2.7': 0.31; 'anyone': 0.31; 'text': 0.33; 'plain': 0.33; 'raw': 0.33; 'display': 0.35; 'something': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'described': 0.36; 'height': 0.36; 'skip:f 40': 0.36; 'next': 0.36; 'url:org': 0.36; 'should': 0.36; 'skip:& 10': 0.38; 'jason': 0.38; 'does': 0.39; 'skip:p 20': 0.39; 'university': 0.39; 'how': 0.40; 'hope': 0.61; 'url:3': 0.61; 'here:': 0.62; 'high': 0.63; 'occur': 0.65; 'skip:f 50': 0.65; 'to:addr:gmail.com': 0.65; 'here': 0.66; 'determine': 0.67; 'wave': 0.74; 'low': 0.83 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=0btte1XGcBwqdAaek17fRlPgz0d1iJgtbKGI6rSBBAw=; b=drvmq+nhXwRs7cNmc//zVvuj2kw4uaFK3ALhYachDm2Z70H7tqlJL1WoNfYkDctcIV BW9moymiKp66Nic9bjoNI9GxAGeVtJRxKrFrLXsLxj+B0boX13AVbbd2Q198hXGsFGUZ j3FKiSpN8mVzR2plcHWXIuslp3Pqt5NcTZ6xsaSSDjQ4hAoWTkkCVFIsnV7Y4Avmlx7S 2UMFOk7gE1Q8X96ZYuuOgQ+BJabOycgHdZyDU2zP0Wk39wHwVzAlJiVvAkcHtoMqAUhm K5ryKh0druhxx2jUTcnulyam0PADR5Omahg1iFAwXWrZifCXABodSGuUHo0mX3doI/Qt qFeA== MIME-Version: 1.0 X-Received: by 10.236.172.170 with SMTP id t30mr25867929yhl.136.1400675416936; Wed, 21 May 2014 05:30:16 -0700 (PDT) In-Reply-To: <47d85028-12c7-4e5b-b540-33a12539fbd2@googlegroups.com> References: <47d85028-12c7-4e5b-b540-33a12539fbd2@googlegroups.com> Date: Wed, 21 May 2014 08:30:16 -0400 Subject: Re: Adding R squared value to scatter plot From: Jason Swails To: Jamie Mitchell Content-Type: multipart/alternative; boundary=20cf30426ef0ac183904f9e828b9 Cc: python list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 269 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1400675425 news.xs4all.nl 2870 [2001:888:2000:d::a6]:60928 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:71853 --20cf30426ef0ac183904f9e828b9 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable =E2=80=8B=E2=80=8B On Wed, May 21, 2014 at 7:59 AM, Jamie Mitchell wrote: > 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=3DnetCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/controlperiod/= south_west/swhcontrol_swest_annavg1D.nc','r') > hs_Q0_con_sw=3Dswh_Q0_con_sw.variables['hs'][:] > > swh_Q3_con_sw=3DnetCDF4.Dataset('/data/cr1/jmitchel/Q3/swh/controlperiod/= south_west/swhcontrol_swest_annavg1D.nc','r') > hs_Q3_con_sw=3Dswh_Q3_con_sw.variables['hs'][:] > > swh_Q4_con_sw=3DnetCDF4.Dataset('/data/cr1/jmitchel/Q4/swh/controlperiod/= south_west/swhcontrol_swest_annavg1D.nc','r') > hs_Q4_con_sw=3Dswh_Q4_con_sw.variables['hs'][:] > > swh_Q14_con_sw=3DnetCDF4.Dataset('/data/cr1/jmitchel/Q14/swh/controlperio= d/south_west/swhcontrol_swest_annavg1D.nc','r') > hs_Q14_con_sw=3Dswh_Q14_con_sw.variables['hs'][:] > > swh_Q16_con_sw=3DnetCDF4.Dataset('/data/cr1/jmitchel/Q16/swh/controlperio= d/south_west/swhcontrol_swest_annavg1D.nc','r') > hs_Q16_con_sw=3Dswh_Q16_con_sw.variables['hs'][:] > > swh_Q0_fut_sw=3DnetCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/2050s/south_we= st/swh2050s_swest_annavg1D.nc','r') > hs_Q0_fut_sw=3Dswh_Q0_fut_sw.variables['hs'][:] > > swh_Q3_fut_sw=3DnetCDF4.Dataset('/data/cr1/jmitchel/Q3/swh/2050s/south_we= st/swh2050s_swest_annavg1D.nc','r') > hs_Q3_fut_sw=3Dswh_Q3_fut_sw.variables['hs'][:] > > swh_Q4_fut_sw=3DnetCDF4.Dataset('/data/cr1/jmitchel/Q4/swh/2050s/south_we= st/swh2050s_swest_annavg1D.nc','r') > hs_Q4_fut_sw=3Dswh_Q4_fut_sw.variables['hs'][:] > > swh_Q14_fut_sw=3DnetCDF4.Dataset('/data/cr1/jmitchel/Q14/swh/2050s/south_= west/swh2050s_swest_annavg1D.nc','r') > hs_Q14_fut_sw=3Dswh_Q14_fut_sw.variables['hs'][:] > > swh_Q16_fut_sw=3DnetCDF4.Dataset('/data/cr1/jmitchel/Q16/swh/2050s/south_= west/swh2050s_swest_annavg1D.nc','r') > hs_Q16_fut_sw=3Dswh_Q16_fut_sw.variables['hs'][:] > > fit_Q0_sw=3Dnp.polyfit(hs_Q0_con_sw,hs_Q0_fut_sw,1) > fit_fn_Q0_sw=3Dnp.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=3D'Q0 no pert'= ) > > fit_Q3_sw=3Dnp.polyfit(hs_Q3_con_sw,hs_Q3_fut_sw,1) > fit_fn_Q3_sw=3Dnp.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=3D'Q3 low sens= ') > > fit_Q4_sw=3Dnp.polyfit(hs_Q4_con_sw,hs_Q4_fut_sw,1) > fit_fn_Q4_sw=3Dnp.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=3D'Q4 low sens= ') > > fit_Q14_sw=3Dnp.polyfit(hs_Q14_con_sw,hs_Q14_fut_sw,1) > fit_fn_Q14_sw=3Dnp.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=3D'Q14 high > sens') > > fit_Q16_sw=3Dnp.polyfit(hs_Q16_con_sw,hs_Q16_fut_sw,1) > fit_fn_Q16_sw=3Dnp.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=3D'Q16 high > sens') > > plt.legend(loc=3D'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 o= f > best fits that I have made. > > Does anyone know how to do this with matplotlib? > =E2=80=8BYou can add plain text or annotations with arrows using any of the= API functions described here: http://matplotlib.org/1.3.1/users/text_intro.html(information specifically regarding the text call is here: http://matplotlib.org/1.3.1/api/pyplot_api.html#matplotlib.pyplot.text) You can also use LaTeX typesetting here, so you can make the text something like r'$R^2$' to display R^2 with "nice" typesetting. (I typically use raw strings for matplotlib text strings with LaTeX formulas in them since LaTeX makes extensive use of the \ character.) The onus is on you, the programmer, to determine _where_ on the plot you want the text to appear. Since you know what you are plotting, you can write a quick helper function that will compute the optimal (to you) location for the label to occur based on where things are drawn on the canvas. There is a _lot_ of flexibility here so you should be able to get your text looking exactly how (and where) you want it. Hope this helps, Jason --=20 Jason M. Swails BioMaPS, Rutgers University Postdoctoral Researcher --20cf30426ef0ac183904f9e828b9 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
= =E2=80=8B=E2=80=8B



On Wed, May 21, 2014 at 7:59 AM, Jamie Mitchell <jamiemitchell= 1604@gmail.com> wrote:
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=3DnetCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/controlperio= d/south_west/swhcontrol_swest_annavg1D.nc','r')
hs_Q0_con_sw=3Dswh_Q0_con_sw.variables['hs'][:]
swh_Q3_con_sw=3DnetCDF4.Dataset('/data/cr1/jmitchel/Q3/swh/controlperio= d/south_west/swhcontrol_swest_annavg1D.nc','r')
hs_Q3_con_sw=3Dswh_Q3_con_sw.variables['hs'][:]
swh_Q4_con_sw=3DnetCDF4.Dataset('/data/cr1/jmitchel/Q4/swh/controlperio= d/south_west/swhcontrol_swest_annavg1D.nc','r')
hs_Q4_con_sw=3Dswh_Q4_con_sw.variables['hs'][:]
swh_Q14_con_sw=3DnetCDF4.Dataset('/data/cr1/jmitchel/Q14/swh/controlper= iod/south_west/swhcontrol_swest_annavg1D.nc','r')
hs_Q14_con_sw=3Dswh_Q14_con_sw.variables['hs'][:]
swh_Q16_con_sw=3DnetCDF4.Dataset('/data/cr1/jmitchel/Q16/swh/controlper= iod/south_west/swhcontrol_swest_annavg1D.nc','r')
hs_Q16_con_sw=3Dswh_Q16_con_sw.variables['hs'][:]
swh_Q0_fut_sw=3DnetCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/2050s/south_= west/swh2050s_swest_annavg1D.nc','r')
hs_Q0_fut_sw=3Dswh_Q0_fut_sw.variables['hs'][:]
swh_Q3_fut_sw=3DnetCDF4.Dataset('/data/cr1/jmitchel/Q3/swh/2050s/south_= west/swh2050s_swest_annavg1D.nc','r')
hs_Q3_fut_sw=3Dswh_Q3_fut_sw.variables['hs'][:]
swh_Q4_fut_sw=3DnetCDF4.Dataset('/data/cr1/jmitchel/Q4/swh/2050s/south_= west/swh2050s_swest_annavg1D.nc','r')
hs_Q4_fut_sw=3Dswh_Q4_fut_sw.variables['hs'][:]
swh_Q14_fut_sw=3DnetCDF4.Dataset('/data/cr1/jmitchel/Q14/swh/2050s/sout= h_west/swh2050s_swest_annavg1D.nc','r')
hs_Q14_fut_sw=3Dswh_Q14_fut_sw.variables['hs'][:]
swh_Q16_fut_sw=3DnetCDF4.Dataset('/data/cr1/jmitchel/Q16/swh/2050s/sout= h_west/swh2050s_swest_annavg1D.nc','r')
hs_Q16_fut_sw=3Dswh_Q16_fut_sw.variables['hs'][:]

fit_Q0_sw=3Dnp.polyfit(hs_Q0_con_sw,hs_Q0_fut_sw,1)
fit_fn_Q0_sw=3Dnp.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=3D'Q= 0 no pert')

fit_Q3_sw=3Dnp.polyfit(hs_Q3_con_sw,hs_Q3_fut_sw,1)
fit_fn_Q3_sw=3Dnp.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=3D'Q= 3 low sens')

fit_Q4_sw=3Dnp.polyfit(hs_Q4_con_sw,hs_Q4_fut_sw,1)
fit_fn_Q4_sw=3Dnp.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=3D'Q= 4 low sens')

fit_Q14_sw=3Dnp.polyfit(hs_Q14_con_sw,hs_Q14_fut_sw,1)
fit_fn_Q14_sw=3Dnp.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=3D= 9;Q14 high sens')

fit_Q16_sw=3Dnp.polyfit(hs_Q16_con_sw,hs_Q16_fut_sw,1)
fit_fn_Q16_sw=3Dnp.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=3D= 9;Q16 high sens')

plt.legend(loc=3D'best')
plt.xlabel('Significant Wave Height annual averages NW Scotland 1981-20= 10')
plt.ylabel('Significant Wave Height annual averages NW Scotland 2040-20= 69')
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?

<= /div>
=E2=80=8BYou c= an add plain text or annotations with arrows using any of the API functions= described here:=C2=A0http://matplotlib.org/1.3.1/users/text_intro.html (information= specifically regarding the text call is here:=C2=A0http://matplotl= ib.org/1.3.1/api/pyplot_api.html#matplotlib.pyplot.text)

You can also use LaTeX types= etting here, so you can make the text something like r'$R^2$' to di= splay R^2 with "nice" typesetting. (I typically use raw strings f= or matplotlib text strings with LaTeX formulas in them since LaTeX makes ex= tensive use of the \ character.)

The onus is on you, the prog= rammer, to determine _where_ on the plot you want the text to appear. =C2= =A0Since you know what you are plotting, you can write a quick helper funct= ion that will compute the optimal (to you) location for the label to occur = based on where things are drawn on the canvas. =C2=A0There is a _lot_ of fl= exibility here so you should be able to get your text looking exactly how (= and where) you want it.

Hope this helps,
Jason

--
Jason M. Swails
BioMaPS,
Rutg= ers University
Postdoctoral Researcher
--20cf30426ef0ac183904f9e828b9--