# How to plot certain rows of a Pandas dataframe using Matplotlib?

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

To plot certain rows of a Pandas dataframe, we can take the following steps −

• Set the figure size and adjust the padding between and around the subplots.
• Create a Pandas data frame, df. It should be a two-dimensional, size-mutable, potentially heterogeneous tabular data.
• Make rows of Pandas plot. Use iloc() function to slice the df and print specific rows.
• To display the figure, use show() method.

## Example

from matplotlib import pyplot as plt
import numpy as np
import pandas as pd

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

df = pd.DataFrame(np.random.randn(10, 5), columns=list('abcde'))
df.iloc[0:6].plot(y='e')

print(df.iloc[0:6])

# plt.show()

## Output

We have 10 rows in the dataframe. When we execute the code, it will print the first 6 rows on the console because iloc[0:6] slices the first 6 rows from the dataframe.

      a            b          c          d             e
0 1.826023    0.606137    0.389687    -0.497605     0.164785
1 0.571941    2.324981   -1.154445     0.757724     0.570713
2 -1.328481   1.248171   -0.849694    -1.133029    -0.977927
3 -0.509296   1.086251    0.809288     0.409166    -0.080694
4 0.973164    1.328212    0.858214     0.997309    -0.375427
5 1.014649    1.480790   -1.451903    -0.306659    -0.382312

To plot this sliced dataframe, uncomment the last line plt.show() in the code and execute it again. 