Skip to content

Unpack labeled data alternative #5053

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 63 commits into from
Sep 13, 2015

Conversation

tacaswell
Copy link
Member

Supersedes #4829

I plan to self-merge this after travis passes (and no sooner than after dinner).

tacaswell and others added 30 commits August 31, 2015 12:06
Try to grab `y.index` before returning `np.arange(len(y))`
After discussions with Brian Granger, Fernando Perez Peter Wang, Matthew
Rocklin and Jake VanderPlas this is a proposal for how to deal with
labeled data in matplotlib.

The approach taken is that if the optional kwarg 'data' is passed in any
other string args/kwargs to the function are replaced be the result
`data[k]` if the key exists in `data`, else leave the value as-is.
If white list is provided, only try to replace those.
This version uses inspect to get the parameter names of a function
and only needs the list of to-be-replaced argument names. In case
inspect can't get a list of parameter names (e.g. *args is used),
the developer has to supply a the complete list of argument names.

This approach only needs for most cases only the list of
replacements and only in the worst case (*args) a additional list
of the names of all positional parameters and not synced two lists
of position and names.

It also makes some "import/ compile time" checks which should warn
devs when the decorator is used with wrong / insufficient arguments.

This also includes the "guess the label"-feature.
None seems the be used much more often than 'y'
Not done: long lines between 80...100

Test code was auto-reformatted in pycharm with max line length 100
If the input data has a `name` attribute and a label is in explicitly
passed in, use the name as the label that will be used in automatic
calls to `legend`.
Now variable length *args can be named and individually replaced.

This unfortunately does not help for plot() because some abiguity as there
is no way to know in some cases if a users wants to get something replaced or not:

```python
data = {"x":X,"y":Y, "c":C}
plot(x,y,c,x,y,c, data=data)
plot(x,y,x,y,x,y, data=data)
```

Here the first call will get all args replaced and matplotlib would
then see all 6 arrays which would translate into three lines with the
same color information (like the second call). In some cases this can
be catched (like for one/three lines with 'x,y,c' combos -> 3/9 args,
will end up with one "missing" if all are treated as replaceable) but
in some cases it's not possible without guesswork. :-(

But in other settings, where the number of args determines the order/names,
this works.

Also activate the tests for the first time...
tacaswell and others added 23 commits August 31, 2015 12:06
In python > 3.2 `functools.update_wrapper` (which backs the `wraps`
decorator) adds `__wrapped__` attribute to the returned function to
provide easy access to the base function.  Mock this up for legacy
versions of python.
pie was a method, which did not work with a data kwarg as it
didn't have a **kwargs catch all which would make pyplot.pie pass
this argument to the decorated function.

Document that the decorated functions are tested in the axes test
cases.
Arbitrary long args, i.e., plot("x","y","r","x2","y2","b") were problematic if
used with a data kwarg and the color spec ("r", "b") was included in data: this
made it impossible in some cases to determine what the user wanted to plot:

plot("x","y","r","x2","y2","b", data={..., "r":..., "b":...)

could be interpreted as

plot(x, y, "r") # all points red
plot(x2, y2, "b") # all points black

or

plot(x,y)
plot(r,x2)
plot(y2,b)

This could lead to hard to debug problems if both styles result in a similar plot
(e.g. if all are values in the same range). Therefore it was decided to remove
this possibility so that the usere gets a proper error message instead:

matplotlib#4829 (comment)

There is still a case of ambiguity (plot("y", "ro", data={"y":..., "ro":...),
which is now detected and a warning is issued. This detection could theoretically
be used to detect the above case as well, but there were so many corner cases,
that the checks became too horrible and I took that out again.

Note that passing in data directly (without a data kwarg) is unaffected, it still
accepts arbitrary long args.
Also make a unit test more specific, as it triggered a warning which
wasn't intended in that case. The warning is tested a few lines later...
IPython ships a version of `Signature` and friends that runs on python
>=2.7.  Fall back to this version of things for python <=3.2.
MNT: use IPython's signature if needed + available
If the second arguement to `plot` is both in data and a valid style code
warn the user.
@tacaswell
Copy link
Member Author

@JanSchulz If you get to this before I merge this and you care, feel free to merge this into your branch and I will merge from your PR.

@jankatins
Copy link
Contributor

@tacaswell done :-)

Seems you fixed all outstanding issues. Thanks and sorry that I wasn't fast enough :-/

@tacaswell tacaswell merged commit 0b4fc7c into matplotlib:master Sep 13, 2015
@tacaswell tacaswell deleted the unpack_labeled_data_alternative branch September 13, 2015 00:28
@@ -11,6 +11,7 @@
from numpy import ma

import matplotlib
from matplotlib import unpack_labeled_data

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am getting an import error for this line when I am using py2exe. Do you know a workaround for this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please file a bug report, since this feature has already been
merged?

On Sun, Mar 20, 2016 at 4:37 PM, mihitha nutakki notifications@github.com
wrote:

In lib/matplotlib/axes/_axes.py
#5053 (comment):

@@ -11,6 +11,7 @@
from numpy import ma

import matplotlib
+from matplotlib import unpack_labeled_data

This is not working


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
https://github.com/matplotlib/matplotlib/pull/5053/files/367b68fbb7added330160e426830116cd247212a..34b879abb0b1c2976dac630d77289020e81494e5#r56771530

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants