2

I have a pandas dataframe where I'm trying to plot two of its columns for which I do:

from matplotlib import pyplot as plt
import numpy as np    
fig, ax = plt.subplots()
df.plot(x = 'x', y = 'y1', ax = ax, label = 'y1', marker='.')
df.plot(x = 'x', y = 'y2', ax = ax, label = 'y2', marker='.')

The problem happens when I'm trying two plot a third variable (y3) along with those pandas columns. y3 calculated doing the following:

z = np.polyfit(df['x'].values, df['y2'].values, 3)
f = np.poly1d(z)
y3 = f(df['y2'].values)

I have used the following two approached to add this to my previous plot:

ax.plot(x = df['x'].values, y = y3, label = 'estimated', marker = '^')

this does not throw any exception but I can not see the new line added to my plot so basically generates the same plot. I have also tried:

plt.plot(x = df['x'].values, y = y3, label = 'estimated', marker = '^', ax = ax)

which throws:

TypeError: inner() got multiple values for keyword argument 'ax'

How would I add that third line to my plot using values stored in y3 which by the way is a numpy array?

7
  • 2
    Try ax.plot(df['x'].values, y3, label = 'estimated', marker = '^') by removing x= and y= Commented Dec 18, 2018 at 15:56
  • 1
    For matplotlib's plot, x and y are positional arguments. For pandas plot they are optional named (keyword-) arguments. Commented Dec 18, 2018 at 16:03
  • @ImportanceOfBeingErnest: This could be important information to the readers. Do you mind if I post both comments as an answer? Commented Dec 18, 2018 at 16:04
  • If this is the first question where someone stumbles upon this problem, you should provide an answer. If it isn't you can mark as duplicate. Commented Dec 18, 2018 at 16:07
  • @ImportanceOfBeingErnest: Thanks for the response. I will post as an answer for the moment. If someone comes up with a duplicate, I would be simply delete my answer and mark it as a duplicate. Commented Dec 18, 2018 at 16:10

1 Answer 1

2

For the first two plots, you use your DataFrame df to directly plot the columns using df.plot() where the x and y are required as keyword (optional) arguments (i.e., works without x= and y= as well) (official docs). So using x=... and y=... works for df.plot().

However, in your third plot command, you are using the axis instance ax for plotting using ax.plot() where you are just using the DataFrame values as arguments. ax.plot() accepts x and y values as positional arguments as clarified by ImportanceOfBeingErnest.

So to answer your problem, you need to use

ax.plot(df['x'].values, y3, label = 'estimated', marker = '^')

by removing x= and y= from your plot command.

The same way, following would work too

plt.plot(df['x'].values, y3, label = 'estimated', marker = '^')

where plt refers to the current axis object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.