3

I have a pandas data frame that contains polynomial approximations of various functions at given points, with variable degrees to the polynomial approximation. It is arranged so that the first column is the function name, the second is the x value, then columns 2-5 are the approximations with a polynomial of the corresponding degree. I would like to make 1 plot for each function showing the convergence of the approximations to that function. I know one way to do it is to break the data frame into separate data frames based on the first column name, but wanted to know if there was a more elegant way to do it.

Edit for clarification: So in the data frame, there are two unrelated functions, say a and b. The second column contains the x values, then third and fourth are functions of x. So it might look like

   fnctn  x  y1  y2
0     a  1   2   3
1     a  2   3   2
2     a  3   4   3
3     a  4   3   4
4     a  5   2   3
5     b  1   1   2
6     b  2   4   6
7     b  3   9   12
8     b  4   16  20
9     b  5   25  30

I would want a plot of y1 and y2 where the first column is a on one plot, and on another plot y1 and y2 where the first column is b

1 Answer 1

3
import pandas
from matplotlib import pyplot as plt
df = pandas.DataFrame({'fnctn':['a','a','a','b','b','b'],'x':[1,2,3,1,2,3],'y1':[2,3,4,3,2,2],'y2':[3,2,3,4,3,2]})

In [19]: df
Out[19]: 
  fnctn  x  y1  y2
0     a  1   2   3
1     a  2   3   2
2     a  3   4   3
3     b  1   3   4
4     b  2   2   3
5     b  3   2   2

for f in set(df['fnctn']): 
     df[df['fnctn']==f].plot(x='x')

a b

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

2 Comments

Thank you, but I probably should have clarified the question more. I want to do that, but for each function in the first column on a separate graph.
Modified answer accordingly!

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.