Str Categorical Axis Support #6689

Merged
merged 2 commits into from Jul 8, 2016

Conversation

Projects
None yet
2 participants
Member

story645 commented Jul 5, 2016 edited

Cleaned up and updated version of #6612, this adds support for directly plotting lists of strings, so that the following is now possible:

index

data = {'apples':10, 'oranges':15, 'lemons':5, 'limes':20}
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.bar(list(data.keys()), list(data.values()), align='center', color='lightgray')
ax.xaxis.set_tick_params(size=0)

index

cat = ["bored", "happy", "bored", "bored", "happy", "bored"]
dog = ["happy", "happy", "happy", "happy", "bored", "happy"]
time = ["combing", "drinking","feeding", "napping", "playing", "washing"]

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(time, dog, label="dog")
ax.plot(time, cat, label="cat")
ax.legend()

Now fighting with the update ticks (@tacaswell) use case:

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(['a', 'b'])
ax.plot(['a', 'b', 'd'])
ax.plot(['b', 'c', 'd'])

'cause it breaks in randomly different ways without the other tests failing.

And beats my why, but Python2.7 triggers weirdness with the bounds & view_limits that Python3.5 doesn't:

_axes.py", line 4023, in scatter: self.autoscale_view()
/axes/_base.py", line 2283, in autoscale_view: margins['left'], margins['right'], self.set_xbound)
axes/_base.py", line 2278, in handle_single_axis: set_bound(x0, x1)
axes/_base.py", line 2763, in set_xbound: self.set_xlim(lower, upper, auto=None)
axes/_base.py", line 2830, in set_xlim: left = self.convert_xunits(left)
artist.py", line 197, in convert_xunits: return ax.xaxis.convert_units(x)
axis.py", line 1500, in convert_units: ret = self.converter.convert(x, self.units, self)
category.py", line 24, in convert: vals = vmap[value]
KeyError: -1.1049590217242939

this was 'caused by plt.plot('a')

story645 self-assigned this Jul 5, 2016

story645 removed the In Progress label Jul 5, 2016

@tacaswell tacaswell commented on an outdated diff Jul 6, 2016

lib/matplotlib/axes/_axes.py
@@ -21,7 +21,9 @@
import matplotlib.collections as mcoll
import matplotlib.colors as mcolors
import matplotlib.contour as mcontour
+import matplotlib.category as _ # <-registers a date unit converter
@tacaswell

tacaswell Jul 6, 2016

Owner

typo in comment.

@tacaswell tacaswell and 1 other commented on an outdated diff Jul 6, 2016

lib/matplotlib/categorical.py
@@ -0,0 +1,86 @@
+"""
@tacaswell

tacaswell Jul 6, 2016

Owner

This file looks like it get renamed and then rebase fought back?

@story645

story645 Jul 6, 2016

Member

Ugh, probably. Rebase and I are not friends.

@tacaswell tacaswell commented on an outdated diff Jul 6, 2016

lib/matplotlib/units.py
units and units conversion. Use cases include converters for custom
objects, e.g., a list of datetime objects, as well as for objects that
-are unit aware. We don't assume any particular units implementation,
-rather a units implementation must provide a ConversionInterface, and
-the register with the Registry converter dictionary. For example,
+are unit aware. We don't assume any particular units implementation;
+<<<<<<< HEAD
+rather a units implementation must provide the register with the Registry
+=======
@tacaswell

tacaswell Jul 6, 2016

Owner

merge detris

@tacaswell tacaswell commented on an outdated diff Jul 6, 2016

lib/matplotlib/category.py
+ @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):
@tacaswell

tacaswell Jul 6, 2016

Owner

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 None and put logic in the function like

if old_map is None:
    old_map = []

tacaswell added this to the 2.1 (next point release) milestone Jul 7, 2016

Owner

tacaswell commented Jul 8, 2016

Can you revert the merge of master into this branch?

Owner

tacaswell commented Jul 8, 2016

Ah, I see there are a bunch of them, I will see if I can de-tangle this.

@story645 @tacaswell story645 Basic support for plotting lists of strings/categorical data. Support…
… for updating ticks/animation in progress/buggy, 'especially for scatter.
b184842
Owner

tacaswell commented Jul 8, 2016

If you look at the category branch on my gh I have cherry-picked off all of your commits and rebased them onto current master.

The changes to the code are exactly the same, but the history is cleaner

22:15 $ git diff story645/category tacaswell/category

shows that there are no code changes.

I suggest you do the following

  • make a new branch on your current branch (just to be safe)
  • reset your local category branch to be my cleaned one git reset --hard tacaswell/category
  • push that branch to your gh git push --force

If you have not discovered magit (https://github.com/magit/magit) yet, I can not suggest it highly enough.

@story645 story645 categorical axis support with np1.6 hack
8a96281
Owner

tacaswell commented Jul 8, 2016

appveyor failure is spurious (failed upload, tests passed).

@tacaswell tacaswell merged commit 5c1e64d into matplotlib:master Jul 8, 2016

2 of 3 checks passed

continuous-integration/appveyor/pr AppVeyor build failed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage increased (+0.04%) to 70.316%
Details

tacaswell removed the needs_review label Jul 8, 2016

Owner

tacaswell commented Jul 8, 2016

appveyor failure is spurious (failed upload, tests passed).

The plan is to collect a single what_new entry + examples towards the end.

Owner

tacaswell commented Jul 8, 2016

🎆 🎉 🎆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment