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


Groups > comp.lang.python > #71853

Re: Adding R squared value to scatter plot

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 <jason.swails@gmail.com>
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&gt;': 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 <jason.swails@gmail.com>
To Jamie Mitchell <jamiemitchell1604@gmail.com>
Content-Type multipart/alternative; boundary=20cf30426ef0ac183904f9e828b9
Cc python list <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.10186.1400675425.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

​​



On Wed, May 21, 2014 at 7:59 AM, Jamie Mitchell <jamiemitchell1604@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=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?
>

​You 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

-- 
Jason M. Swails
BioMaPS,
Rutgers University
Postdoctoral Researcher

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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