Matplotlib - Line Plot

Viewed 32

I am trying to plot an array of 101 rows * 12 Columns, with row #1 as a highlight using the code below:

plt.plot(HW.transpose()[1:101],color = 'grey', alpha = 0.1)
plt.plot(HW.transpose()[0],color = 'red', linewidth = 3, alpha = 0.7)

The only issue in this graph is that 'S1' somehow ends up in the last instead of beginning. What am I doing wrong?

enter image description here

1 Answers

HW.transpose()[1:101] doesn't select the desired columns. You can use HW.transpose().iloc[:, 1:101] instead:

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

HW = pd.DataFrame(np.random.randn(101, 12).cumsum(axis=1), columns=[f'S{i}' for i in range(1, 13)])

plt.plot(HW.transpose().iloc[:, 1:101], color='grey', alpha=0.1)
plt.plot(HW.transpose().iloc[:, 0], color='red', linewidth=3, alpha=0.7)

plt.show()

example plot

Related