Skip to content

plt.plot fails if given x and y kwargs #4059

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

Closed
Garrett-R opened this issue Feb 1, 2015 · 17 comments
Closed

plt.plot fails if given x and y kwargs #4059

Garrett-R opened this issue Feb 1, 2015 · 17 comments

Comments

@Garrett-R
Copy link

When I execute the following command:

python3 -c "import matplotlib.pyplot as plt; plt.plot(x=[0,1], y=[0,1]); plt.show()"

it outputs:

Empty_Plot


Edit: I see my error. I was not supposed to provide the data as keyword arguments. Indeed plt.plot([0,1], [0,1]) works fine.

I don't know anything about how matplotlib works, but perhaps plot should be able to handle this (similar to how pandas.DataFrame.plot can handle this):

import pandas as pd
df = pd.DataFrame([[0,0],[1,1]], columns=['A', 'B'])
df.plot('A', 'B')
df.plot(x='A', y='B')

If that's not desirable, then perhaps it could at least throw an error or give a warning that an unexpected keyword argument was given...

@blink1073
Copy link
Member

@Garrett-R, plot does not take x and y as keyword arguments. This works:

python3 -c "import matplotlib.pyplot as plt; plt.plot([0,1], [0,1]); plt.show()"

@Garrett-R Garrett-R changed the title Data not showing up in plot plt.plot fails if given x and y kwargs Feb 1, 2015
@Garrett-R
Copy link
Author

@blink1073, thanks I must have realized it at the same time, because I just edited my original post. Please see my new comments.

@blink1073
Copy link
Member

@Garrett-R, the problem with your suggestion is that plot can take multiple sets of arguments, for example:

python3 -c "import matplotlib.pyplot as plt; plt.plot([0,1], [0,1], 'b', [0, .8], [0, 1], 'r'); plt.show()"

@blink1073
Copy link
Member

But perhaps the x and y kwargs could just be treated as if they were the first set of args...

@blink1073
Copy link
Member

With this change your original example works:

diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py
index 940ebdd..7db782f 100644
--- a/lib/matplotlib/axes/_axes.py
+++ b/lib/matplotlib/axes/_axes.py
@@ -1365,6 +1365,12 @@ class Axes(_AxesBase):
             self.cla()
         lines = []

+        if 'y' in kwargs:
+            args = (kwargs.pop('y'),) + args
+
+        if 'x' in kwargs:
+            args = (kwargs.pop('x'),) + args
+
         for line in self._get_lines(*args, **kwargs):
             self.add_line(line)
             lines.append(line)

@Garrett-R
Copy link
Author

I like it! 👍

I wonder if there's any downsides to that. I know the upside. I spent about an hour trying to debug my code, and had to read the the matplotlib source before realizing the x and y kwargs weren't being looked at.

@blink1073
Copy link
Member

It could certainly be made more robust, but I agree it is a nice add. I'll let one of the core devs chime in.

@tacaswell
Copy link
Member

The plot api is modeled on the MATLAB api and far too magical, but changing it to simplify it is a non-starter (because it would break too much code) and I am not a huge fan of adding any more magic to it.

One concern I have about @blink1073 's patch is that if there is a x kwarg, but no y it will erroneously convert the x -> y which would be bad.

My knee jerk reaction is 👎 , but am open to being convinced other wise.

Also, I don't think any of the docs say that you can pass in the x/y values via kwargs (if there are, please point me to them so they can be removed).

@tacaswell tacaswell added this to the unassigned milestone Feb 1, 2015
@blink1073
Copy link
Member

@tacaswell, that's why I said it needed more clean up. 😄
I don't consider it magic if it is documented and intuitive, which I think this would be. A simple change to check for the presence of x before using y would cover your concern.

@blink1073
Copy link
Member

The fact that I could not immediately look at @Garrett-R's example and see why it would fail is a good indicator...

@WeatherGod
Copy link
Member

I share thomas's concern. I have repeatedly made attempts to refactor that
code, and failed. My failure to fix it is the main source for my imposter
syndrome in this project...

If changes are made here, then where else should it go? scatter()? bar()?
loglog()? What about polar() with kwargs of "r" and "theta"?

Nah, I think the real problem here is that there was no error reported. No
data was supplied! I know I am a huge fan of ensuring that the plotting
functions work with empty inputs, but I wouldn't go so far as working for
non-existent inputs!

-1

On Sun, Feb 1, 2015 at 11:15 AM, Steven Silvester notifications@github.com
wrote:

The fact that I could not immediately look at @Garrett-R
https://github.com/Garrett-R's example and see why it would fail is a
good indicator...


Reply to this email directly or view it on GitHub
#4059 (comment)
.

@blink1073
Copy link
Member

Fair enough. Then, there are two problems: there should be at least one arg given to these functions, or they should raise an error. The other problem is that there is no handling of invalid kwargs (they're just ignored, a Python-wide problem with the **kwargs construct).

@tacaswell
Copy link
Member

It is made extra bad in mpl due to the way that we pass things up (down) the stack so we can not do validation at most levels. I suspect the right place to blow up is in the Artist constructor.

@WeatherGod I recently dove into the plot arg parsing code a bit (69decec which has an astoundingly high commit message length / number of lines changed ratio) and there is no shame in getting bogged down/confused by that code!

@petehuang
Copy link
Contributor

Given the reactions from @tacaswell and @WeatherGod, should we close this with no intent to proceed?

@WeatherGod
Copy link
Member

Well, we should probably still raise an error if plot() doesn't see any arguments.

@efiring
Copy link
Member

efiring commented Jan 6, 2017

It might even not be too difficult to handle the no-args case with x and/or y kwargs, bypassing all the logic required to handle the Matlab-style parsing.

@anntzer
Copy link
Contributor

anntzer commented Jul 1, 2019

This now errors out with "TypeError: plot got an unexpected keyword argument 'x'".
It was argued in #11526 that we should not support keyword arguments in this case, so I'll just close this.

@anntzer anntzer closed this as completed Jul 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants