0

I have a sample data frame shown below. I want to plot each column against the data frame index.

x1 = pd.DataFrame()
x1['x25'] = df['X'][0:45].reset_index(drop=True)
x1['x50'] = df['X'][90:135].reset_index(drop=True)
x1['x75'] = df['X'][180:225].reset_index(drop=True)
x1['x100'] = df['X'][270:315].reset_index(drop=True)
x1['x125'] = df['X'][360:405].reset_index(drop=True)  

using x1.head() the output is shown below.

    x25      x50      x75    x100   x125
0   22732   22852   22997   23151   23253
1   22732   22853   22995   23153   23254
2   22733   22851   22997   23153   23254
3   22731   22851   22995   23150   23255
4   22730   22851   22997   23152   23254

I checked the output of each column, they are all equal.

print(len(x1.index), len(x1['x25']), len(x1['x50']), len(x1['x75']), len(x1['x100']), len(x1['x125']))

45 45 45 45 45 45

I am trying to plot with the command below, but i am getting The error message ValueError: arrays must all be same length

sns.lineplot( x1, x1.index, ['x25','x50','x75','x100','x125'])

could somebody please let me know, what i am doing wrong.

1

1 Answer 1

1

Consider calling lineplot multiple times, passing in object such as pandas series to named arguments:

sns.lineplot(x=x1.index, y=x1['x25'])
sns.lineplot(x=x1.index, y=x1['x50'])
sns.lineplot(x=x1.index, y=x1['x75'])
sns.lineplot(x=x1.index, y=x1['x100'])
sns.lineplot(x=x1.index, y=x1['x125'])

Or in loop:

for i in  ['x25','x50','x75','x100','x125']:
   sns.lineplot(x=x1.index, y=x1[i])

Multiple Line Plots Output


However, consider using a single data frame and hence single lineplot call by melting your wide data to long and rendering your index as a column. Then call lineplot with hue for automatic legend:

# CREATE NEW COLUMN NAMED index
x1 = x1.reset_index()

# MELT DATA
mdf = x1.melt(id_vars='index')

# PLOT LINE WITH data AND hue ARGUMENTS
sns.lineplot(x='index', y='value', data=mdf, hue='variable')

Single Data Frame Plot


Data

df = pd.DataFrame({'X': np.random.uniform(2000, 5000, 500)})

x1 = pd.DataFrame({'x25': df['X'][0:45].reset_index(drop=True),
                   'x50': df['X'][90:135].reset_index(drop=True),
                   'x75': df['X'][180:225].reset_index(drop=True),
                   'x100': df['X'][270:315].reset_index(drop=True),
                   'x125': df['X'][360:405].reset_index(drop=True)})
Sign up to request clarification or add additional context in comments.

Comments

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.