0

I am trying to use the .plot() function in pandas to plot data into a line graph.

The data sorted by date with 48 rows after each date. Sample below:

 1 2 ... 46 47 48 18 2018-02-19 1.317956 1.192840 ... 1.959250 1.782985 1.418093 19 2018-02-20 1.356267 1.192248 ... 2.123432 1.760629 1.569340 20 2018-02-21 1.417181 1.288694 ... 2.086715 1.823581 1.612062 21 2018-02-22 1.431536 1.279514 ... 2.201972 1.878109 1.694159 

etc until row 346.

I tried the below but .plot does not seem to take positional arguments:

df.plot(x=df.iloc[0:346,0],y=[0:346,1:49] 

How would I go about plotting my rows by date (the 1st column) on a line graph and can I expand this to include multiple axis?

1 Answer 1

0

There are multiple ways to do this, some of which are directly through the pandas dataframe. However, given your sample plotting line, I think the easiest might be to just use matplotlib directly:

import matplotlib.pyplot as plt plt.plot(df.iloc[0:346,0],df.iloc[0:346,1:49]) 

For multiple axes you can add a few lines to make subplots. For example:

import matplotlib.pyplot as plt fig = plt.figure() ax1 = plt.subplot(1,2,1) plt.plot(df.iloc[0:346,0],df.iloc[0:346,1:10],ax=ax1) ax2 = plt.subplot(1,2,2) plt.plot(df.iloc[0:346,0],df.iloc[0:346,20:30],ax=ax2) 

You can also do this using the pandas plot() function that you were trying to use - it also takes an ax argument the same way as above, where you can provide the axis on which to plot. If you want to stick to pandas, I think you'd be best off setting the index to be a datetime index (see this link as an example: https://stackoverflow.com/a/27051371/12133280) and then using df.plot('column_name',ax=ax1). The x axis will be the index, which you would have set to be the date.

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

2 Comments

When I run this I unfortunately get the error: TypeError: plot got an unexpected keyword argument 'x'. I have imported all dependences so this is confusing.
Sorry you're right. I edited it. The first two arguments are x and y, but it doesn't work to say x= and y=. Hopefully it works now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.