21

I have a pandas dataframe with three columns and I am plotting each column separately using the following code:

data.plot(y='value')

Which generates a figure like this one:

enter image description here

What I need is a subset of these values and not all of them. For example, I want to plot values at rows 500 to 1000 and not from 0 to 3500. Any idea how I can tell the plot function to only pick those?

Thanks

2 Answers 2

31

use iloc to slice your df:

data.iloc[499:999].plot(y='value')

this will slice from row 500 up to but not including row 1000

Example:

In [35]:
df = pd.DataFrame(np.random.randn(10,2), columns=list('ab'))
df.iloc[2:6]

Out[35]:
          a         b
2  0.672884  0.202798
3  0.514998  1.744821
4 -1.982109 -0.770861
5  1.364567  0.341882

df.iloc[2:6].plot(y='b')

enter image description here

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

Comments

-1

enter image description hereIn case you have two dataframes say, 'tst' and 'ptst' and want to display only certain values of both (same range in both) on the same graph. Following will help:

import matplotlib.pyplot as plts

plts.figure(figsize=(10,5))
plts.plot(tst['Price'])
plts.plot(ptst['Price'])

1 Comment

How does it plot a certain number of rows ?

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.