-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Str Categorical Axis Support #6689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Basic support for plotting lists of strings/categorical data. Support…
… for updating ticks/animation in progress/buggy, 'especially for scatter.
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
""" | ||
catch all for categorical functions | ||
""" | ||
from __future__ import (absolute_import, division, print_function, | ||
unicode_literals) | ||
|
||
import six | ||
import numpy as np | ||
|
||
import matplotlib.units as units | ||
import matplotlib.ticker as ticker | ||
|
||
|
||
class StrCategoryConverter(units.ConversionInterface): | ||
@staticmethod | ||
def convert(value, unit, axis): | ||
"""Uses axis.unit_data map to encode | ||
data as integers | ||
""" | ||
|
||
if isinstance(value, six.string_types): | ||
return dict(axis.unit_data)[value] | ||
|
||
vals = np.asarray(value, dtype='str') | ||
for label, loc in axis.unit_data: | ||
vals[vals == label] = loc | ||
return vals.astype('float') | ||
|
||
@staticmethod | ||
def axisinfo(unit, axis): | ||
seq, locs = zip(*axis.unit_data) | ||
majloc = StrCategoryLocator(locs) | ||
majfmt = StrCategoryFormatter(seq) | ||
return units.AxisInfo(majloc=majloc, majfmt=majfmt) | ||
|
||
@staticmethod | ||
def default_units(data, axis): | ||
# the conversion call stack is: | ||
# default_units->axis_info->convert | ||
axis.unit_data = map_categories(data, axis.unit_data) | ||
return None | ||
|
||
|
||
def map_categories(data, old_map=[], sort=True): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't put mutables in the signatures as defaults, it can lead to some fun closures. def foo(d=[]):
d.append(len(d))
print(d) gives In [24]: foo()
[0]
In [25]: foo()
[0, 1]
In [26]: foo()
[0, 1, 2]
In [27]: foo()
[0, 1, 2, 3] Either make this an empty tuple or make the default if old_map is None:
old_map = [] |
||
"""Create mapping between unique categorical | ||
values and numerical identifier. | ||
|
||
Paramters | ||
--------- | ||
data: iterable | ||
sequence of values | ||
old_map: list of tuple, optional | ||
if not `None`, than old_mapping will be updated with new values and | ||
previous mappings will remain unchanged) | ||
sort: bool, optional | ||
sort keys by ASCII value | ||
|
||
Returns | ||
------- | ||
list of tuple | ||
[(label, ticklocation),...] | ||
|
||
""" | ||
|
||
# code typical missing data in the negative range because | ||
# everything else will always have positive encoding | ||
# question able if it even makes sense | ||
spdict = {'nan': -1, 'inf': -2, '-inf': -3} | ||
|
||
# cast all data to str | ||
strdata = [str(d) for d in data] | ||
|
||
uniq = set(strdata) | ||
|
||
category_map = old_map.copy() | ||
|
||
if old_map: | ||
olabs, okeys = zip(*old_map) | ||
olabs, okeys = set(olabs), set(okeys) | ||
svalue = max(okeys) + 1 | ||
else: | ||
olabs, okeys = set(), set() | ||
svalue = 0 | ||
|
||
new_labs = (uniq - olabs) | ||
|
||
missing = (new_labs & set(spdict.keys())) | ||
category_map.extend([(m, spdict[m]) for m in missing]) | ||
|
||
new_labs = (new_labs - missing) | ||
if sort: | ||
new_labs = list(new_labs) | ||
new_labs.sort() | ||
|
||
new_locs = range(svalue, svalue + len(new_labs)) | ||
category_map.extend(list(zip(new_labs, new_locs))) | ||
return category_map | ||
|
||
|
||
class StrCategoryLocator(ticker.FixedLocator): | ||
def __init__(self, locs): | ||
super(StrCategoryLocator, self).__init__(locs, None) | ||
|
||
|
||
class StrCategoryFormatter(ticker.FixedFormatter): | ||
def __init__(self, seq): | ||
super(StrCategoryFormatter, self).__init__(seq) | ||
|
||
|
||
# Connects the convertor to matplotlib | ||
units.registry[bytearray] = StrCategoryConverter() | ||
units.registry[str] = StrCategoryConverter() | ||
|
||
if six.PY3: | ||
units.registry[bytes] = StrCategoryConverter() | ||
elif six.PY2: | ||
units.registry[unicode] = StrCategoryConverter() |
Binary file not shown.
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo in comment.