R – emulate the default behavior of hist() with ggplot2 for bin width

Without sample data, it’s always difficult to get reproducible results, so i’ve created a sample dataset set.seed(16) mydata <- data.frame(myvariable=rnorm(500, 1500000, 10000)) #base histogram hist(mydata$myvariable) As you’ve learned, hist() is a generic function. If you want to see the different implementations you can type methods(hist). Most of the time you’ll be running hist.default. So if … Read more

Fitting a histogram with python

Here you have an example working on py2.6 and py3.2: from scipy.stats import norm import matplotlib.mlab as mlab import matplotlib.pyplot as plt # read data from a text file. One number per line arch = “test/Log(2)_ACRatio.txt” datos = [] for item in open(arch,’r’): item = item.strip() if item != ”: try: datos.append(float(item)) except ValueError: pass … Read more

2D and 3D Scatter Histograms from arrays in Python

Here it follows two functions: hist2d_bubble and hist3d_bubble; that may fit for your purpose: import numpy as np import matplotlib.pyplot as pyplot from mpl_toolkits.mplot3d import Axes3D def hist2d_bubble(x_data, y_data, bins=10): ax = np.histogram2d(x_data, y_data, bins=bins) xs = ax[1] ys = ax[2] points = [] for (i, j), v in np.ndenumerate(ax[0]): points.append((xs[i], ys[j], v)) points = … Read more

fast 2dimensional histograming in matlab

Here is my version for a 2D histogram: %# some random data X = randn(2500,1); Y = randn(2500,1)*2; %# bin centers (integers) xbins = floor(min(X)):1:ceil(max(X)); ybins = floor(min(Y)):1:ceil(max(Y)); xNumBins = numel(xbins); yNumBins = numel(ybins); %# map X/Y values to bin indices Xi = round( interp1(xbins, 1:xNumBins, X, ‘linear’, ‘extrap’) ); Yi = round( interp1(ybins, 1:yNumBins, … Read more

Different breaks per facet in ggplot2 histogram

Here is one alternative: hls <- mapply(function(x, b) geom_histogram(data = x, breaks = b), dlply(d, .(par)), myBreaks) ggplot(d, aes(x=x)) + hls + facet_wrap(~par, scales = “free_x”) If you need to shrink the range of x, then hls <- mapply(function(x, b) { rng <- range(x$x) bb <- c(rng[1], b[rng[1] <= b & b <= rng[2]], rng[2]) … Read more

Logarithmic y-axis bins in python

try plt.yscale(‘log’, nonposy=’clip’) http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.yscale The issue is with the bottom of bars being at y=0 and the default is to mask out in-valid points (log(0) -> undefined) when doing the log transformation (there was discussion of changing this, but I don’t remember which way it went) so when it tries to draw the rectangles for … Read more