Re: Matplotlib Contour Plots
On Tuesday, August 19, 2014 10:21:48 PM UTC+1, pec...@pascolo.net wrote: Jamie Mitchell jamiemitchell1...@gmail.com writes: You were right Christian I wanted a shape (2,150). Thank you Rustom and Steven your suggestion has worked. Unfortunately the data doesn't plot as I imagined. What I would like is: X-axis - hs_con_sw Y-axis - te_con_sw Z-axis - Frequency What I would like is for the Z-axis to contour the frequency or amount of times that the X-axis data and Y-axis data meet at a particular point or bin. Does anyone know what function or graph could best show this? in my understanding, you have 3 arrays of data that describe 3D data points, and you want to draw a 2D contour plot... in this case you have to interpolate the z-values on a regular grid, that's very easy if you already know what to do ;-) here I assume that data is in a .csv file % cat a.csv 0 ≤ x ≤ 10, 0 ≤ y ≤ 10, z = cos(sqrt((x-5)**2_(y-5)**2)) 1.922065,5.827944,-0.998953 7.582322,0.559370,0.411861 5.001753,3.279957,-0.148694 ... of course my z's are different from yours, but this shouldn't be a real problem --- and here it is my *tested* solution (tested on python 2.7, that is), please feel free to adapt to your needs hth, ciao g % cat contour.py from numpy import loadtxt, linspace from matplotlib.mlab import griddata import matplotlib.pyplot as pl # open 'a.csv', specify the delimiter, specify how many header rows, # slurp the data temp_array = loadtxt(open('a.csv'),delimiter=',',skiprows=1) # the shape of temp_array is (N,3), we want its transpose temp_array = temp_array.transpose() # now the shape is (3,N) and we can do unpack and assignment: x, y, z = temp_array # now the tricky part, # 1: create two arrays with 101 (arbitrary number) equispaced values # between 0 and 10 --- that is the ranges of data x and data y xi = linspace(0,10,101) yi = linspace(0,10,101) # 2: create, by interpolation, the 2D array that contourf so eagerly # awaited! print griddata.__doc__ zi = griddata(x,y,z, xi,yi) # eventually, lets plot the stuff... # see http://matplotlib.org/examples/pylab_examples/griddata_demo.html # for further details and ideas pl.contour (xi,yi,zi,11,linewidths=1,colors='black') pl.contourf(xi,yi,zi); pl.colorbar() # optional pl.gca().set_aspect('equal', 'box') pl.show() % python contour.py This is great and works very well - thank you!! -- https://mail.python.org/mailman/listinfo/python-list
Re: Matplotlib Contour Plots
You were right Christian I wanted a shape (2,150). Thank you Rustom and Steven your suggestion has worked. Unfortunately the data doesn't plot as I imagined. What I would like is: X-axis - hs_con_sw Y-axis - te_con_sw Z-axis - Frequency What I would like is for the Z-axis to contour the frequency or amount of times that the X-axis data and Y-axis data meet at a particular point or bin. Does anyone know what function or graph could best show this? Thanks for all your help, Jamie -- https://mail.python.org/mailman/listinfo/python-list
Re: Matplotlib Contour Plots
On Friday, August 15, 2014 4:13:26 PM UTC+1, Steven D'Aprano wrote: Jamie Mitchell wrote: I created the 2D array which read as: That's not a 2D array. When the amount of data you have is too big to clearly see what it happening, replace it with something smaller. Instead of 30 items per sub-array, try it with 5 items per sub-array. Instead of eight decimal places, try it with single-digit integers. Anything to make it small enough to see clearly. When I do that with your data, instead of this: array([[[ 2.0886, 2.29400015, 2.00400019, 1.8811, 2.0480001 , 2.16800022, 2.0480001 , 1.8829, 1.9586, 2.0029, 2.02800012, 1.8124, 1.9505, 1.96200013, 1.95200014, 1.99800014, 2.0717, 1.8829, 1.9849, 2.1346, 2.1148, 1.8945, 2.0519, 2.0198, 2.03400016, 2.16600013, 2.0099, 1.86200011, 2.19800019, 2.0128]], [[ 8.515 , 8.8811, 8.5519, 7.9481, 8.6066, 8.515 , 8.8019, 8.1311, 8.6858, 8.7254, 8.4754, 8.25 , 8.4085, 8.4358, 8.3839, 8.3566, 8.6339, 8.5123, 8.3689, 8.6981, 8.5273, 8.1339, 8.3689, 8.4208, 8.5547, 8.7254, 9.0915, 8.1858, 8.7623, 8.5396]]], dtype=float32) I get this: array([[[ 2, 2, 2, 1, 2]], [[ 8, 8, 8, 7, 8]]], dtype=float32) which is much easier to work with. See the difference between that smaller example, and my earlier explanation of the difference between a 1D and 2D array? One dimensional arrays are made from a single list of numbers: [...] Two dimensional arrays are made from a list of lists: [ [...], [...] ] *Three* dimensional arrays are made from a list of lists of lists: [ [ [...], [...] ] ] *Four* dimensional arrays are made from a list of lists of lists of lists: [ [ [ [...], [...] ] ] ] and so on. You have a 3D array, with dimensions 2 x 1 x 30. You can check the dimensions by storing the array into a variable like this: py a = numpy.array([[[ 2, 2, 2, 1, 2]], [[ 8, 8, 8, 7, 8]]]) py a.shape (2, 1, 5) -- Steven Thanks for your suggestions Steven. Unfortunately I still can't make the plot I'm looking for. Do you mind if I go back to the start? Sorry I'm probably not explaining what I need very well. So I have two 1D arrays: 1st array - ([8, 8.8,8.5,7.9,8.6 ...], dtype=float32) It has a shape (150,) 2nd array - ([2, 2.2, 2.5, 2.3, ...],dtype=float32) It has a shape (150,) What I want to do is create a 2D array which merges the 1st and 2nd array so that I would have: ([[8, 8.8,8.5,7.9,8.6 ...],[2,2,2,2,5,2.3, ...]], dtype=float32) that would have a shape (150,150) In this form I could then plot a 2D contour. Thanks for your patience. Jamie -- https://mail.python.org/mailman/listinfo/python-list
Re: Matplotlib Contour Plots
I forgot to mention that when I try: a=np.array([[hs_con_sw],[te_con_sw]]) I get a 3D shape for some reason - (2,1,150) which is not what I'm after. Thanks, Jamie -- https://mail.python.org/mailman/listinfo/python-list
Re: Matplotlib Contour Plots
On Thursday, August 14, 2014 5:53:09 PM UTC+1, Steven D'Aprano wrote: Jamie Mitchell wrote: Hello all, I want to contour a scatter plot but I don't know how. Can anyone help me out? Certainly. Which way did you come in? :-) Sorry, I couldn't resist. It took me literally 20 seconds to find this by googling for matplotlib contour plot, and it only took that long because I misspelled contour the first time. http://matplotlib.org/examples/pylab_examples/contour_demo.html Does this help? If not, please explain what experience you have with matplotlib, what you have tried, what you expected it to do, and what it did instead. -- Steven Yep I've seen that thanks but I can't get it to work. I don't have much experience with matplotlib or programming in general. I just want to get a contour plot of two numpy arrays. When I call plt.contour on my data I get input must be a 2D array An example of one of my arrays: array([ 2.0886, 2.29400015, 2.00400019, 1.8811, 2.0480001 , 2.16800022, 2.0480001 , 1.8829, 1.9586, 2.0029, 2.02800012, 1.8124, 1.9505, 1.96200013, 1.95200014, 1.99800014, 2.0717, 1.8829, 1.9849, 2.1346, 2.1148, 1.8945, 2.0519, 2.0198, 2.03400016, 2.16600013, 2.0099, 1.86200011, 2.19800019, 2.0128], dtype=float32) How do I get the above array in to the right format for a contour plot? Thanks, Jamie -- https://mail.python.org/mailman/listinfo/python-list
Re: Matplotlib Contour Plots
On Friday, August 15, 2014 2:23:25 PM UTC+1, Steven D'Aprano wrote: Jamie Mitchell wrote: [...] I just want to get a contour plot of two numpy arrays. When I call plt.contour on my data I get input must be a 2D array You are providing a 1D array, or possibly a 3D array. So the question you really want to ask is not How do I do contour plots but how do I make a 2D array? An example of one of my arrays: array([ 2.0886, 2.29400015, 2.00400019, 1.8811, 2.0480001 , 2.16800022, 2.0480001 , 1.8829, 1.9586, 2.0029, 2.02800012, 1.8124, 1.9505, 1.96200013, 1.95200014, 1.99800014, 2.0717, 1.8829, 1.9849, 2.1346, 2.1148, 1.8945, 2.0519, 2.0198, 2.03400016, 2.16600013, 2.0099, 1.86200011, 2.19800019, 2.0128], dtype=float32) How do I get the above array in to the right format for a contour plot? Here's an example of making a 2D array: py import numpy py a = numpy.array([1.2, 2.5, 3.7, 4.8]) # One dimensional array py a array([ 1.2, 2.5, 3.7, 4.8]) py b = numpy.array([ [1.2, 2.5, 3.7, 4.8], ... [9.5, 8.1, 7.0, 6.2] ]) # Two dimensional array py b array([[ 1.2, 2.5, 3.7, 4.8], [ 9.5, 8.1, 7. , 6.2]]) One dimensional arrays are made from a single list of numbers: [...] Two dimensional arrays are made from a list of lists: [ [...], [...] ] -- Steven Thank you Steven. I created the 2D array which read as: array([[[ 2.0886, 2.29400015, 2.00400019, 1.8811, 2.0480001 , 2.16800022, 2.0480001 , 1.8829, 1.9586, 2.0029, 2.02800012, 1.8124, 1.9505, 1.96200013, 1.95200014, 1.99800014, 2.0717, 1.8829, 1.9849, 2.1346, 2.1148, 1.8945, 2.0519, 2.0198, 2.03400016, 2.16600013, 2.0099, 1.86200011, 2.19800019, 2.0128]], [[ 8.515 , 8.8811, 8.5519, 7.9481, 8.6066, 8.515 , 8.8019, 8.1311, 8.6858, 8.7254, 8.4754, 8.25 , 8.4085, 8.4358, 8.3839, 8.3566, 8.6339, 8.5123, 8.3689, 8.6981, 8.5273, 8.1339, 8.3689, 8.4208, 8.5547, 8.7254, 9.0915, 8.1858, 8.7623, 8.5396]]], dtype=float32) Unfortunately when I called plt.contour on this, it said again Input must be a 2D array. Is there something I have missed? Thanks, Jamie -- https://mail.python.org/mailman/listinfo/python-list
Matplotlib Contour Plots
Hello all, I want to contour a scatter plot but I don't know how. Can anyone help me out? Cheers, Jamie -- https://mail.python.org/mailman/listinfo/python-list
Contouring a 2D histogram
Hi all, I have plotted a 2D histogram like so: python2.7 import netCDF4 import iris import iris.palette import numpy as np import matplotlib as mpl import matplotlib.cm as cm import matplotlib.mlab as mlab import matplotlib.pyplot as plt from matplotlib.colors import from_levels_and_colors fig=plt.figure() nbins=20 nice_cmap=plt.get_cmap('brewer_RdYlBu_11') colors=nice_cmap([5,6,7,8,9,10]) levels=[1,2,3,4,5] cmap, norm=from_levels_and_colors(levels, colors, extend='both') H, xedges, yedges=np.histogram2d(te_Q0_con_sw,hs_Q0_con_sw,bins=nbins) Hmasked=np.ma.masked_where(H==0,H) plt.pcolormesh(xedges,yedges,Hmasked,cmap=cmap,norm=norm,label='Q0 control') # From this I get a 'scattered' 2D histogram. Does anyone know how I can contour that scatter? Thanks, Jamie -- https://mail.python.org/mailman/listinfo/python-list
Matplotlib Colouring outline of histogram
Hi folks, Instead of colouring the entire bar of a histogram i.e. filling it, I would like to colour just the outline of the histogram. Does anyone know how to do this? Version - Python2.7 Cheers, Jamie -- https://mail.python.org/mailman/listinfo/python-list
Problem with numpy 2D Histogram
Hi folks, I'm trying to plot a 2D histogram but I'm having some issues: from pylab import * import numpy as np import netCDF4 hist,xedges,yedges=np.histogram2d(x,y,bins=10) extent=[xedges[0],xedges[-1],yedges[0],yedges[-1]] imshow(hist.T,extent=extent,interpolation='nearest') colorbar() show() After the first line of code I get: TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe' I'm using python2.7, x and y are type 'numpy.ndarray' Cheers, Jamie -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem with numpy 2D Histogram
On Friday, June 20, 2014 10:25:44 AM UTC+1, Peter Otten wrote: Jamie Mitchell wrote: Hi folks, I'm trying to plot a 2D histogram but I'm having some issues: from pylab import * import numpy as np import netCDF4 hist,xedges,yedges=np.histogram2d(x,y,bins=10) extent=[xedges[0],xedges[-1],yedges[0],yedges[-1]] imshow(hist.T,extent=extent,interpolation='nearest') colorbar() show() After the first line of code I get: TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe' I'm using python2.7, x and y are type 'numpy.ndarray' The error message complains about the dtype, i. e. the type of the elements in the array, not the array itself. Make sure the elements are floating point numbers or something compatible, not arbitrary Python objects. As a baseline the following works from pylab import * import numpy as np x, y = np.random.randn(2, 100) print x, type(x), x.dtype print y, type(y), y.dtype hist, xedges, yedges = np.histogram2d(x, y, bins=10) extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]] imshow(hist.T, extent=extent, interpolation='nearest') colorbar() show() while this doesn't: #... x, y = np.random.randn(2, 100) import decimal y = np.array([decimal.Decimal.from_float(v) for v in y]) #... Thanks Peter. I have changed my x and y data to float64 types but I am still getting the same error message? Cheers, Jamie -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem with numpy 2D Histogram
On Friday, June 20, 2014 12:00:15 PM UTC+1, Peter Otten wrote: Jamie Mitchell wrote: I have changed my x and y data to float64 types but I am still getting the same error message? Please double-check by adding assert x.dtype == np.float64 assert y.dtype == np.float64 If none of these assertions fail try to make a minimal script including some data that provokes the TypeError and post it here. OK this is my code: swh_Q0_con_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/controlperiod/south_west/swhcontrol_swest_annavg.nc','r') hs_Q0_con_sw=swh_Q0_con_sw.variables['hs'][:] x=hs_Q0_con_sw.astype(float64) # When I print the dtype of x here it says 'float64' mwp_Q0_con_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q0/mean_wave_period/south_west/controlperiod/mwpcontrol_swest_annavg1D.nc','r') te_Q0_con_sw=mwp_Q0_con_sw.variables['te'][:] y=te_Q0_con_sw.astype(float64) If I try assert x.dtype == np.float64 I get: AssertionError hist,xedges,yedges=np.histogram2d(x,y,bins=10) TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe' Thanks, Jamie -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem with numpy 2D Histogram
On Friday, June 20, 2014 9:46:29 AM UTC+1, Jamie Mitchell wrote: Hi folks, I'm trying to plot a 2D histogram but I'm having some issues: from pylab import * import numpy as np import netCDF4 hist,xedges,yedges=np.histogram2d(x,y,bins=10) extent=[xedges[0],xedges[-1],yedges[0],yedges[-1]] imshow(hist.T,extent=extent,interpolation='nearest') colorbar() show() After the first line of code I get: TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe' I'm using python2.7, x and y are type 'numpy.ndarray' Cheers, Jamie Thanks for your help Peter. -- https://mail.python.org/mailman/listinfo/python-list
Re: Matplotlib Colouring outline of histogram
On Friday, June 20, 2014 2:47:03 PM UTC+1, Jason Swails wrote: On Fri, Jun 20, 2014 at 4:10 AM, Jamie Mitchell jamiemit...@gmail.com wrote: Hi folks, Instead of colouring the entire bar of a histogram i.e. filling it, I would like to colour just the outline of the histogram. Does anyone know how to do this? Version - Python2.7 Look at the matplotlib.pyplot.hist function documentation: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.hist In addition to the listed parameters, you'll see the Other Parameters taken are those that can be applied to the created Patch objects (which are the actual rectangles). For the Patch keywords, see the API documentation on the Patch object (http://matplotlib.org/api/artist_api.html#matplotlib.patches.Patch). So you can do one of two things: 1) Pass the necessary Patch keywords to effect what you want e.g. (untested): import matplotlib.pyplot as plt plt.hist(dataset, bins=10, range=(-5, 5), normed=True, edgecolor='b', linewidth=2, facecolor='none', # Patch options ) plt.show() 2) Iterate over the Patch instances returned by plt.hist() and set the properties you want. e.g. (untested): import matplotlib.pyplot as plt n, bins, patches = plt.hist(dataset, bins=10, range=(-5, 5), normed=True) for patch in patches: patch.set_edgecolor('b') # color of the lines around each bin patch.set_linewidth(2) # Set width of bin edge patch.set_facecolor('none') # set no fill # Anything else you want to do plt.show() Approach (1) is the easy way, and is there to satisfy the majority of use cases. However, approach (2) is _much_ more flexible. Suppose you wanted to highlight a particular region of your data with a specific facecolor or edgecolor -- you can apply the features you want to individual patches using approach (2). Or if you wanted to highlight a specific bin with thicker lines. This is a common theme in matplotlib -- you can use keywords to apply the same features to every part of a plot or you can iterate over the drawn objects and customize them individually. This is a large part of what makes matplotlib nice to me -- it has a simple mode as well as a predictable API for customizing a plot in almost any way you could possibly want. HTH, Jason -- Jason M. Swails BioMaPS, Rutgers University Postdoctoral Researcher That's great Jason thanks for the detailed response, I went with the easier option 1! I am also trying to put hatches on my histograms like so: plt.hist(dataset,bins=10,hatch=['*']) When it comes to plt.show() I get the following error message: File /usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/backends/backend_gtk.py, line 435, in expose_event self._render_figure(self._pixmap, w, h) File /usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/backends/backend_gtkagg.py, line 84, in _render_figure FigureCanvasAgg.draw(self) File /usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/backends/backend_agg.py, line 451, in draw self.figure.draw(self.renderer) File /usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/artist.py, line 55, in draw_wrapper draw(artist, renderer, *args, **kwargs) File /usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/figure.py, line 1034, in draw func(*args) File /usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/artist.py, line 55, in draw_wrapper draw(artist, renderer, *args, **kwargs) File /usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/axes.py, line 2086, in draw a.draw(renderer) File /usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/artist.py, line 55, in draw_wrapper draw(artist, renderer, *args, **kwargs) File /usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/patches.py, line 429, in draw renderer.draw_path(gc, tpath, affine, rgbFace) File /usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/backends/backend_agg.py, line 145, in draw_path self._renderer.draw_path(gc, path, transform, rgbFace) File /usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/backend_bases.py, line 1010, in get_hatch_path return Path.hatch(self._hatch, density) File /usr/local/sci/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/path.py, line 888, in hatch hatch_path = cls._hatch_dict.get((hatchpattern, density)) TypeError: unhashable type: 'list' Traceback (most recent call
Re: Matplotlib Colouring outline of histogram
On Friday, June 20, 2014 9:10:58 AM UTC+1, Jamie Mitchell wrote: Hi folks, Instead of colouring the entire bar of a histogram i.e. filling it, I would like to colour just the outline of the histogram. Does anyone know how to do this? Version - Python2.7 Cheers, Jamie Great thanks again Jason. -- https://mail.python.org/mailman/listinfo/python-list
Re: Matplotlib - specifying bin widths
On Thursday, June 5, 2014 4:54:16 PM UTC+1, Jamie Mitchell wrote: Hello all! Instead of setting the number of bins I want to set the bin width. I would like my bins to go from 1.7 to 2.4 in steps of 0.05. How do I say this in the code? Cheers, Jamie That's great thanks Mark. -- https://mail.python.org/mailman/listinfo/python-list
Overlaying a boxplot onto a time series figure
Hi there, I would like to overlay some boxplots onto a time series. I have tried pylab.hold(True) in between the two plots in my code but this hasn't worked. The problem is that the x-axes of the boxplots and the time series are not the same. Code for time series: 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'][:] year_con=swh_Q0_con_sw.variables['year'][:] 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'][:] year_fut=swh_Q0_fut_sw.variables['year'][:] 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_con_sw=np.polyfit(year_con,hs_Q0_con_sw,1) fit_fn_Q0_con_sw=np.poly1d(fit_Q0_con_sw) plt.plot(year_con,hs_Q0_con_sw,'g.') plt.plot(year_con,fit_fn_Q0_con_sw(year_con),'g',label='Q0 no pert') fit_Q3_con_sw=np.polyfit(year_con,hs_Q3_con_sw,1) fit_fn_Q3_con_sw=np.poly1d(fit_Q3_con_sw) plt.plot(year_con,hs_Q3_con_sw,'b.') plt.plot(year_con,fit_fn_Q3_con_sw(year_con),'b',label='Q3 low sens') fit_Q4_con_sw=np.polyfit(year_con,hs_Q4_con_sw,1) fit_fn_Q4_con_sw=np.poly1d(fit_Q4_con_sw) plt.plot(year_con,hs_Q4_con_sw,'y.') plt.plot(year_con,fit_fn_Q4_con_sw(year_con),'y',label='Q4 low sens') fit_Q14_con_sw=np.polyfit(year_con,hs_Q14_con_sw,1) fit_fn_Q14_con_sw=np.poly1d(fit_Q14_con_sw) plt.plot(year_con,hs_Q14_con_sw,'r.') plt.plot(year_con,fit_fn_Q14_con_sw(year_con),'r',label='Q14 high sens') fit_Q16_con_sw=np.polyfit(year_con,hs_Q16_con_sw,1) fit_fn_Q16_con_sw=np.poly1d(fit_Q16_con_sw) plt.plot(year_con,hs_Q16_con_sw,'c.') plt.plot(year_con,fit_fn_Q16_con_sw(year_con),'c',label='Q16 high sens') fit_Q0_fut_sw=np.polyfit(year_fut,hs_Q0_fut_sw,1) fit_fn_Q0_fut_sw=np.poly1d(fit_Q0_fut_sw) plt.plot(year_fut,hs_Q0_fut_sw,'g.') plt.plot(year_fut,fit_fn_Q0_fut_sw(year_fut),'g') fit_Q3_fut_sw=np.polyfit(year_fut,hs_Q3_fut_sw,1) fit_fn_Q3_fut_sw=np.poly1d(fit_Q3_fut_sw) plt.plot(year_fut,hs_Q3_fut_sw,'b.') plt.plot(year_fut,fit_fn_Q3_fut_sw(year_fut),'b') fit_Q4_fut_sw=np.polyfit(year_fut,hs_Q4_fut_sw,1) fit_fn_Q4_fut_sw=np.poly1d(fit_Q4_fut_sw) plt.plot(year_fut,hs_Q4_fut_sw,'y.') plt.plot(year_fut,fit_fn_Q4_fut_sw(year_fut),'y') fit_Q14_fut_sw=np.polyfit(year_fut,hs_Q14_fut_sw,1) fit_fn_Q14_fut_sw=np.poly1d(fit_Q14_fut_sw) plt.plot(year_fut,hs_Q14_fut_sw,'r.') plt.plot(year_fut,fit_fn_Q14_fut_sw(year_fut),'y') fit_Q16_fut_sw=np.polyfit(year_fut,hs_Q16_fut_sw,1) fit_fn_Q16_fut_sw=np.poly1d(fit_Q16_fut_sw) plt.plot(year_fut,hs_Q16_fut_sw,'c.') plt.plot(year_fut,fit_fn_Q16_fut_sw(year_fut),'c') plt.legend(loc='best') plt.xlabel('Year') plt.ylabel('Significant Wave Height annual averages SW England') plt.title('Time series of Significant Wave Height') plt.show() Code for boxplots: python2.7 from pylab import * import netCDF4 data=(hs_Q0_con_sw,hs_Q3_con_sw,hs_Q4_con_sw,hs_Q14_con_sw,hs_Q16_con_sw) figure(1) boxplot(data) labels=('QO no pert','Q3 low sens','Q4 low sens','Q14 high sens','Q16 high sens') xticks(range(1,6),labels,rotation=15) xlabel('Ensemble Member') ylabel('Significant Wave Height Annual Average') title('Significant Wave Height SW England 1981-2010') show() If anybody knows how I could integrate these two plots I would be eternally grateful! Thanks, Jamie -- https://mail.python.org/mailman/listinfo/python-list
Matplotlib - specifying bin widths
Hello all! Instead of setting the number of bins I want to set the bin width. I would like my bins to go from 1.7 to 2.4 in steps of 0.05. How do I say this in the code? Cheers, Jamie -- https://mail.python.org/mailman/listinfo/python-list
Re: Adding R squared value to scatter plot
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 -- https://mail.python.org/mailman/listinfo/python-list
Adding R squared value to scatter plot
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 -- https://mail.python.org/mailman/listinfo/python-list
Saving a file as netCDF4 in Python
Dear all, Apologies as this sounds like a very simple question but I can't find an answer anywhere. I have loaded a netCDF4 file into python as follows: swh=netCDF4.Dataset('path/to/netCDFfile,'r') I then isolate the variables I wish to plot: hs=swh.variables['hs'] year=swh.variables['year'] I would then like to save these hs and year variables so that I don't have to isolate them every time I want to plot them. Any help would be much appreciated. Cheers, Jamie -- https://mail.python.org/mailman/listinfo/python-list
len() of unsized object - ks test
Hello all, I am trying to perform a Kolmogorov-Smirnov test in Python but I'm having a few difficulties. # My files are netCDF so I import them as follows: control=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/controlperiod/south_west/swhcontrol_swest_concatannavg_1D.nc','r') # The string is simply a 1D array # Then performing the ks test: kstest(control,'norm') # I then get the following error: File stdin, line 1, in module File /usr/local/sci/lib/python2.7/site-packages/scipy/stats/stats.py, line 3413, in kstest N = len(vals) TypeError: len() of unsized object Any ideas on why this isn't working would be great. Thanks, Jamie -- https://mail.python.org/mailman/listinfo/python-list
Re: len() of unsized object - ks test
On Friday, April 25, 2014 3:07:54 PM UTC+1, Jamie Mitchell wrote: Hello all, I am trying to perform a Kolmogorov-Smirnov test in Python but I'm having a few difficulties. # My files are netCDF so I import them as follows: control=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/controlperiod/south_west/swhcontrol_swest_concatannavg_1D.nc','r') # The string is simply a 1D array # Then performing the ks test: kstest(control,'norm') # I then get the following error: File stdin, line 1, in module File /usr/local/sci/lib/python2.7/site-packages/scipy/stats/stats.py, line 3413, in kstest N = len(vals) TypeError: len() of unsized object Any ideas on why this isn't working would be great. Thanks, Jamie Thanks for your help. Steven your right I wasn't reading in the file on netCDF4.Dataset, I was just opening it. I have rectified it now - a silly mistake! Thanks again, Jamie -- https://mail.python.org/mailman/listinfo/python-list
Line of best fit
I am new to python so apologies for the ignorance with this question. How would I apply a line of best fit to a plot? My data are netCDF4 data files and this is essentially what I have done so far: swh1=netCDF4.Dataset('filename','r') hs1=swh1.variables['hs'] swh2=netCDF4.Dataset('filename'.'r') hs2=swh2.variables['hs'] plt.plot(hs1,hs2,'.') Cheers, Jamie -- https://mail.python.org/mailman/listinfo/python-list
Memory error
Hello all, I'm afraid I am new to all this so bear with me... I am looking to find the statistical significance between two large netCDF data sets. Firstly I've loaded the two files into python: swh=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/controlperiod/averages/swh_control_concat.nc', 'r') swh_2050s=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/2050s/averages/swh_2050s_concat.nc', 'r') I have then isolated the variables I want to perform the pearson correlation on: hs=swh.variables['hs'] hs_2050s=swh_2050s.variables['hs'] Here is the metadata for those files: print hs type 'netCDF4.Variable' int16 hs(time, latitude, longitude) standard_name: significant_height_of_wind_and_swell_waves long_name: significant_wave_height units: m add_offset: 0.0 scale_factor: 0.002 _FillValue: -32767 missing_value: -32767 unlimited dimensions: time current shape = (86400, 350, 227) print hs_2050s type 'netCDF4.Variable' int16 hs(time, latitude, longitude) standard_name: significant_height_of_wind_and_swell_waves long_name: significant_wave_height units: m add_offset: 0.0 scale_factor: 0.002 _FillValue: -32767 missing_value: -32767 unlimited dimensions: time current shape = (86400, 350, 227) Then to perform the pearsons correlation: from scipy.stats.stats import pearsonr pearsonr(hs,hs_2050s) I then get a memory error: Traceback (most recent call last): File stdin, line 1, in module File /usr/local/sci/lib/python2.7/site-packages/scipy/stats/stats.py, line 2409, in pearsonr x = np.asarray(x) File /usr/local/sci/lib/python2.7/site-packages/numpy/core/numeric.py, line 321, in asarray return array(a, dtype, copy=False, order=order) MemoryError This also happens when I try to create numpy arrays from the data. Does anyone know how I can alleviate theses memory errors? Cheers, Jamie -- https://mail.python.org/mailman/listinfo/python-list
Re: Memory error
On Monday, March 24, 2014 11:32:31 AM UTC, Jamie Mitchell wrote: Hello all, I'm afraid I am new to all this so bear with me... I am looking to find the statistical significance between two large netCDF data sets. Firstly I've loaded the two files into python: swh=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/controlperiod/averages/swh_control_concat.nc', 'r') swh_2050s=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/2050s/averages/swh_2050s_concat.nc', 'r') I have then isolated the variables I want to perform the pearson correlation on: hs=swh.variables['hs'] hs_2050s=swh_2050s.variables['hs'] Here is the metadata for those files: print hs type 'netCDF4.Variable' int16 hs(time, latitude, longitude) standard_name: significant_height_of_wind_and_swell_waves long_name: significant_wave_height units: m add_offset: 0.0 scale_factor: 0.002 _FillValue: -32767 missing_value: -32767 unlimited dimensions: time current shape = (86400, 350, 227) print hs_2050s type 'netCDF4.Variable' int16 hs(time, latitude, longitude) standard_name: significant_height_of_wind_and_swell_waves long_name: significant_wave_height units: m add_offset: 0.0 scale_factor: 0.002 _FillValue: -32767 missing_value: -32767 unlimited dimensions: time current shape = (86400, 350, 227) Then to perform the pearsons correlation: from scipy.stats.stats import pearsonr pearsonr(hs,hs_2050s) I then get a memory error: Traceback (most recent call last): File stdin, line 1, in module File /usr/local/sci/lib/python2.7/site-packages/scipy/stats/stats.py, line 2409, in pearsonr x = np.asarray(x) File /usr/local/sci/lib/python2.7/site-packages/numpy/core/numeric.py, line 321, in asarray return array(a, dtype, copy=False, order=order) MemoryError This also happens when I try to create numpy arrays from the data. Does anyone know how I can alleviate theses memory errors? Cheers, Jamie Just realised that obviously pearson correlation requires two 1D arrays and mine are 3D, silly mistake! -- https://mail.python.org/mailman/listinfo/python-list