Skip to content

Commit

Permalink
fixed bug with distributions with rplot_data
Browse files Browse the repository at this point in the history
  • Loading branch information
robertocalandra committed Aug 31, 2017
1 parent 870476f commit cfdb1fd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
14 changes: 11 additions & 3 deletions examples/rplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,18 @@
x.append(np.random.rand(100, 30))
x.append(np.random.rand(50, 20)+2)
# ---
fig = rplot_data(data=x, color=['red', 'green'])
fig = rplot_data(data=x, color=['red', 'green'], distribution='median+65')
plt.show()


# Raw data
x = []
x.append(np.random.rand(100, 30))
x.append(np.random.rand(50, 20)+2)
# ---
fig = rplot_data(data=x, color=['red', 'green'], distribution='median+65+95')
plt.show()

# Mean vs median
NUM_SAMPLES = 500000 # Number curves within each distribution (e.g., the number of repetitions of each experiment)
NUM_ITER = 10 # Number iterations
Expand All @@ -63,12 +71,12 @@ def randn_skew_fast(N, alpha=0.0, mean=0.0, scale=1.0):
Y.append(np.array(t).transpose())

plt.figure()
fig = rplot_data(data=Y, typeplot='mean+68+95+99', legend=['Skewed', 'Gaussian'])
fig = rplot_data(data=Y, distribution='mean+68+95+99', legend=['Skewed', 'Gaussian'])
ax = plt.gca()
ax.set_title('Mean + percentiles')

plt.figure()
fig = rplot_data(data=Y, typeplot='median+68+95+99', legend=['Skewed', 'Gaussian'])
fig = rplot_data(data=Y, distribution='median+68+95+99', legend=['Skewed', 'Gaussian'])
ax = plt.gca()
ax.set_title('Median + percentiles')
plt.show()
12 changes: 6 additions & 6 deletions scipyplot/plot/gauss_1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
__version__ = '0.51'


def rplot_data(data, x=None, typeplot='mean+68+95+99', legend=None, xlabel=None, ylabel=None, color=None):
def rplot_data(data, x=None, distribution='mean+68+95+99', legend=None, xlabel=None, ylabel=None, color=None):
"""
Plot curves from raw data (wrapper around rplot).
Given a matrix of data, this function automatically compute statistics (such as mean and var) and plot them.
Multiple curves (potentially with different lenght) can be plot simultaneously by passing a list of matrices.
:param data: list of np.matrix
:param x: list of np.array (or single np.array) indicating the x valued for the corresponding data
:param typeplot: String formatted as 'mean' or 'median' followed from the percentiles of the uncertainty curves
:param distribution: String formatted as 'mean' or 'median' followed from the percentiles of the uncertainty curves
(interleaved by '+').
Example 1: 'mean+68+95+99' plot the mean and one shaded area for each corresponding 68/95/99 percentile
Example 2: 'median+68' Plot the median and the 68 percentile
Expand All @@ -38,14 +38,14 @@ def rplot_data(data, x=None, typeplot='mean+68+95+99', legend=None, xlabel=None,
if isinstance(data, np.ndarray):
data = [data]
# Parse data
out = typeplot.split("+")
distribution = "+".join(out[1:])
out = distribution.split("+")
odistribution = "+".join(out[1:])
Y = []
V = []
X = x
for i in range(len(data)):
if out[0] == 'median':
median, quantiles = rstats.median_percentile(data[i])
median, quantiles = rstats.median_percentile(data[i], des_percentiles=odistribution)
Y.append(median)
V.append(quantiles)
if out[0] == 'mean':
Expand All @@ -60,7 +60,7 @@ def rplot_data(data, x=None, typeplot='mean+68+95+99', legend=None, xlabel=None,
# Is it a list then?
assert len(X) == len(Y)

fig = rplot(y=Y, x=X, uncertainty=V, distribution=distribution, xlabel=xlabel, ylabel=ylabel, legend=legend,
fig = rplot(y=Y, x=X, uncertainty=V, distribution=odistribution, xlabel=xlabel, ylabel=ylabel, legend=legend,
color=color)

return fig
Expand Down

0 comments on commit cfdb1fd

Please sign in to comment.