Aaron Huang
Aaron Huang

Reputation: 25

How can I integrate plt.subplots to my code?

I am quite new to python so please bear with me.

My code so far is below:

import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

df = pd.read_csv(r"/Users/aaronhuang/Desktop/ffp/exfileCLEAN2.csv", skiprows=[1])
magnitudes = df['Magnitude '].values
times = df['Time '].values
zscores = np.abs(stats.zscore(magnitudes, ddof=1))
outlier_indicies = np.argwhere(zscores > 3).flatten()
numbers = print(times[outlier_indicies])



window = 2
num = 1
x = times[outlier_indicies[num]- window:outlier_indicies[num]+window+1]
y = magnitudes[outlier_indicies[num]- window:outlier_indicies[num]+window+1]


plt.plot(x, y)
plt.xlabel('Time (units)')
plt.ylabel('Magnitude (units)')
plt.show()
fig = plt.figure()

Currently, the code only prints one graph, determined by num. I would like it to print all the graphs at once, using plt.subplots, which I think is the easiest way.

I would be great if someone could help me integrate plt.subplots as I don't really know where to start.

Thanks

PS: Here is the data if it would be useful.

Upvotes: 0

Views: 44

Answers (1)

r-beginners
r-beginners

Reputation: 35185

Graph created. The cause of the error is an extra space at the end of the column name in the provided CSV file. The code fixes that. If you fixed the column names in the original data, you should also fix the code.

import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

# df = pd.read_csv(r"/Users/aaronhuang/Desktop/ffp/exfileCLEAN2.csv", skiprows=[1])
df = pd.read_csv(r"./exfileCLEAN2.csv", skiprows=[1])
magnitudes = df['Magnitude '].values
times = df['Time '].values
zscores = np.abs(stats.zscore(magnitudes, ddof=1))
outlier_indicies = np.argwhere(zscores > 3).flatten()
numbers = print(times[outlier_indicies])

import matplotlib.pyplot as plt
fig, axes = plt.subplots(6, 10, figsize=(30,30))

for i in range(6):
    for j in range(10):
        x = df.iloc[j*10:(j+1)*10,:]
        axes[i][j].plot(x['Time '], x['Magnitude '])
        axes[i][j].set_xticklabels(x['Time '], rotation=45)

window = 2
num = 1
x = times[outlier_indicies[num] - window:outlier_indicies[num]+window+1]
y = magnitudes[outlier_indicies[num] - window:outlier_indicies[num]+window+1]


plt.plot(x, y)
plt.xlabel('Time (units)')
plt.ylabel('Magnitude (units)')
plt.show()
fig = plt.figure()

enter image description here

Upvotes: 1

Related Questions