Skip to content

Commit

Permalink
update to hist
Browse files Browse the repository at this point in the history
  • Loading branch information
kwinkunks committed Apr 28, 2020
1 parent 80c5d5b commit 318e587
Show file tree
Hide file tree
Showing 4 changed files with 402 additions and 228 deletions.
6 changes: 5 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
Changelog
=========

0.8.4 (current develop)
0.8.4 (April 2020)
----

- Added `label` kwarg to `striplog.plot()`. Give it the name of a field in the `primary` component and it will be added to the plot as text. Experimental feature; might break with some sizes of striplog. Feedback welcome.
- Fixed a bug in ``Striplog.hist()`` which produced the wrong colours and labels for the bars.


0.8.3 (July 2019)
----

- Minor release. Updated tutorials and fixed some bugs.


0.8.2 (July 2019)
-----

Expand Down
4 changes: 2 additions & 2 deletions striplog/markov.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def __init__(self,
"""
self.step = step
self.include_self = include_self
self.observed_counts = np.atleast_2d(observed_counts)
self.observed_counts = np.atleast_2d(observed_counts).astype(int)

if states is not None:
self.states = np.asarray(states)
Expand Down Expand Up @@ -218,7 +218,7 @@ def from_sequence(cls,
if states is None:
states = uniques
else:
states = np.asarray(states)
states = np.asarray(list(states))

O = np.zeros(tuple(states.size for _ in range(step+1)))
for seq in seq_of_seqs:
Expand Down
54 changes: 34 additions & 20 deletions striplog/striplog.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import operator
import warnings
from collections import defaultdict
from collections import OrderedDict
from functools import reduce
from copy import deepcopy

Expand Down Expand Up @@ -2107,13 +2108,15 @@ def thinnest(self, n=1, index=False):
return self[indices]

def hist(self,
lumping=None,
summary=False,
sort=True,
plot=True,
legend=None,
ax=None
):
lumping=None,
summary=False,
sort=True,
plot=True,
legend=None,
ax=None,
rotation=0,
ha='center',
):
"""
Plots a histogram and returns the data for it.
Expand All @@ -2130,6 +2133,10 @@ def hist(self,
legend (Legend): The legend with which to colour the bars.
ax (axis): An axis object, which will be returned if provided.
If you don't provide one, it will be created but not returned.
rotation (int): The rotation angle of the x-axis tick labels.
Default is 0 but -45 is useful.
ha (str): The horizontal alignment of the x-axis tick labels.
Default is 'center' but 'left' is good for -ve rotation.
Returns:
Tuple: A tuple of tuples of entities and counts.
Expand All @@ -2139,9 +2146,7 @@ def hist(self,
"""
# This seems like overkill, but collecting all this stuff gives
# the user some choice about what they get back.
comps = {}
labels = {}
entries = defaultdict(int)
entries = OrderedDict()
for i in self:
if lumping:
k = i.primary[lumping]
Expand All @@ -2150,18 +2155,24 @@ def hist(self,
k = i.primary.summary()
else:
k = i.primary
comps[k] = i.primary
labels[k] = i.primary.summary()
entries[k] += i.thickness
v = entries.get(k, {'thick': 0}).get('thick', 0)

entries[k] = {
'label': i.primary.summary(),
'colour': legend.get_colour(i.primary) if legend else None,
'thick': v + i.thickness,
}

if sort:
allitems = sorted(entries.items(),
key=lambda i: i[1],
reverse=True
)
ents, counts = zip(*allitems)
key=lambda i: i[1]['thick'],
reverse=True
)
ents, data = zip(*allitems)
else:
ents, counts = tuple(entries.keys()), tuple(entries.values())
ents, data = tuple(entries.keys()), tuple(entries.values())

counts = [d['thick'] for d in data]

# Make plot.
if plot:
Expand All @@ -2170,12 +2181,15 @@ def hist(self,
return_ax = False
else:
return_ax = True

ind = np.arange(len(ents))
bars = ax.bar(ind, counts, align='center')
ax.set_xticks(ind)
ax.set_xticklabels(labels.values())
ax.set_xticklabels([d['label'] for d in data],
rotation=rotation,
ha=ha)
if legend:
colours = [legend.get_colour(c) for c in comps.values()]
colours = [d['colour'] for d in data]
for b, c in zip(bars, colours):
b.set_color(c)
ax.set_ylabel('Thickness [m]')
Expand Down
Loading

0 comments on commit 318e587

Please sign in to comment.