Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #71852 > unrolled thread
| Started by | Jamie Mitchell <jamiemitchell1604@gmail.com> |
|---|---|
| First post | 2014-05-21 04:59 -0700 |
| Last post | 2014-06-05 08:55 -0700 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
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
| From | Jamie Mitchell <jamiemitchell1604@gmail.com> |
|---|---|
| Date | 2014-05-21 04:59 -0700 |
| Subject | Adding R squared value to scatter plot |
| Message-ID | <47d85028-12c7-4e5b-b540-33a12539fbd2@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Jason Swails <jason.swails@gmail.com> |
|---|---|
| Date | 2014-05-21 08:30 -0400 |
| Message-ID | <mailman.10186.1400675425.18130.python-list@python.org> |
| In reply to | #71852 |
[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
[toc] | [prev] | [next] | [standalone]
| From | Jamie Mitchell <jamiemitchell1604@gmail.com> |
|---|---|
| Date | 2014-06-05 08:55 -0700 |
| Message-ID | <64970717-b855-4baf-96af-0e4b6f08a6f5@googlegroups.com> |
| In reply to | #71853 |
On Wednesday, May 21, 2014 1:30:16 PM UTC+1, Jason Swails wrote:
>
>
>
>
>
>
> On Wed, May 21, 2014 at 7:59 AM, Jamie Mitchell <jamiemit...@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
Hi Jason,
Thank you for your swift response - you solved my problem!
Sorry I took a while to get back to you.
Thanks again,
Jamie
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web