Reputation: 13510
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?
Upvotes: 2
Views: 1909
Reputation: 39052
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.
Upvotes: 2