1

I have a Pandas DataFrame with displacements for different times (rows) and specific vertical locations (columns names). The goal is to plot the displacements (x axis) for the vertical location (y axis) for a given time (series).

According to the next example (time = 0, 1, 2, 3, 4 and vertical locations = 0.5, 1.5, 2.5, 3.5), how can the displacements be plotted for the times 0 and 3?

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(88)
df = pd.DataFrame({
    'time': np.arange(0, 5, 1),
    '0.5': np.random.uniform(-1, 1, size = 5),
    '1.5': np.random.uniform(-2, 2, size = 5),
    '2.5': np.random.uniform(-3, 3, size = 5),
    '3.5': np.random.uniform(-4, 4, size = 5),
    })
df = df.set_index('time')
1

1 Answer 1

2

You can filter your dataframe to only contain the desired rows. Either by using the positional index

filtered = df.iloc[[0,3],:]

or by using the actualy index of the dataframe,

filtered = df.iloc[(df.index == 3) | (df.index == 0),:]

You can then plot a scatter plot like this:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(88)
df = pd.DataFrame({
    'time': np.arange(0, 5, 1),
    '0.5': np.random.uniform(-1, 1, size = 5),
    '1.5': np.random.uniform(-2, 2, size = 5),
    '2.5': np.random.uniform(-3, 3, size = 5),
    '3.5': np.random.uniform(-4, 4, size = 5),
    })
df = df.set_index('time')


filtered_df = df.iloc[[0,3],:]
#filtered_df = df.iloc[(df.index == 3) | (df.index == 0),:]

loc = list(map(float, df.columns))

fig, ax = plt.subplots()
for row in filtered_df.iterrows():
    ax.scatter(row[1], loc, label=row[1].name)

plt.legend()    
plt.show()

enter image description here

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.