Showing posts with label frustration. Show all posts
Showing posts with label frustration. Show all posts

Saturday, April 13, 2013

Matplotlib: plotting a bar plot with errorbars from data

This didn't make any sense for what I've been trying to do (a histogram), but might be useful someday:

y, binEdges = np.histogram(vel_22,bins=15)
errArray = np.zeros((y.shape))
for i, edge in enumerate(binEdges):
  vel_lo = np.where((vel_22 <= edge) & (vel_22 > binEdges[i-1]))[0]
  print np.mean(vel_err[vel_lo]), 'mean'
  if math.isnan(np.mean(vel_err[vel_lo])):
   errArray[i -1] = 0.
  else:
   errArray[i -1] = np.mean(vel_err[vel_lo])
width = 12
bincenters = 0.5*(binEdges[1:]+binEdges[:-1])
plt.bar(bincenters, y, color='white', edgecolor='k', width=width, yerr=errArray)

Tuesday, March 26, 2013

sqrt(1/2), integers, floats

That may be Python 101, but np.sqrt(1/2) = 0, whereas np.sqrt(0.5) = 0.707...(). I thought integers are automatically cast to floats in such operations.
Except they aren't -- that's what the mysterious line from __future__ import division is supposed to remedy! http://stackoverflow.com/questions/4841732/python-convert-fraction-to-decimal

Thursday, March 14, 2013

Fitting a model to data: lit review

Yesterday I showed some preliminary TF results at our group meeting, and it was rightly noted that the straight line fit I made was wrong. I've only included errors in one variable (velocity, obtained by using bootstrapping) in the fit, and made a mess of that by mixing the variables on the plot.
I've been thinking about how to do a proper fit when the data has uncertainties in both directions, and been revisiting this paper by Hogg et al.:
In the astrophysics literature (see, for example, the Tully Fisher literature, there is a tradition, when there are uncertainties in both directions, of fi tting the "forward" and "reverse" relations, that is, fi tting y as a function of x and then x as a function of y, and then splitting the diff erence between the two slopes so obtained, or treating the di fference between the slopes as a systematic uncertainty. This is unjusti ed.

Friday, January 25, 2013

IDL # operator

I'm playing around with my old MSc code, as I have an idea where it might be useful. Here's something I forgot:
The # operator computes array elements by multiplying the columns of the first array by the rows of the second array. The second array must have the same number of columns as the first array has rows. The resulting array has the same number of columns as the first array and the same number of rows as the second array.

Sunday, December 16, 2012

Matplotlib: errorbar throws a ValueError: too many values to unpack

I've been working on the errorbars in Matplotlib, and kept getting this error.
To avoid it, check your array dimensions: errorbar(), unlike scatterplot(), keeps on insisting on (x, ) error array dimensions.
1D arrays (vectors) in Numpy can be of shape (x, 1) or (x, ). That's usually interchangeable, but in cases like that, np.reshape(x, (x.shape[0], )) does the trick.
I think hope there is a deep underlying reason for that.

Matplotlib: no edge for marker

"edgecolor = None" sets the point edge colour to the default value, which is black. "edgecolor = "none"" removes the border altogether. I can't stand stuff like this.