一次打印多个图形

时间:2020-07-02 04:53:39

标签: python matplotlib

我对python很陌生,所以请多多包涵。

我的代码如下:

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()

打印内容是:

/Users/aaronhuang/.conda/envs/EXTTEst/bin/python 
 "/Users/aaronhuang/PycharmProjects/EXTTEst/Code sandbox.py"
[2455338.895 2455350.644 2455391.557 2455404.776 2455413.734 2455451.661
 2455473.49  2455477.521 2455507.505 2455702.662 2455734.597 2455765.765
 2455776.575 2455826.593 2455842.512 2455866.508 2455996.796 2456017.767
 2456047.694 2456058.732 2456062.722 2456071.924 2456082.802 2456116.494
 2456116.535 2456116.576 2456116.624 2456116.673 2456116.714 2456116.799
 2456123.527 2456164.507 2456166.634 2456391.703 2456455.535 2456455.6
 2456501.763 2456511.616 2456519.731 2456525.49  2456547.588 2456570.526
 2456595.515 2456776.661 2456853.543 2456920.511 2456953.496 2457234.643
 2457250.68  2457252.672 2457278.526 2457451.89  2457485.722 2457497.93
 2457500.674 2457566.874 2457567.877 2457644.495 2457661.553 2457675.513]
Traceback (most recent call last):
  File "/Users/aaronhuang/PycharmProjects/EXTTEst/Code sandbox.py", line 15, in <module>
    xdata = times[outlier_indicies[numbers] - window:outlier_indicies[numbers] + window + 1]
TypeError: only integer scalar arrays can be converted to a scalar index

Process finished with exit code 1

数据框的摘要为:

           Time   Magnitude   Magnitude error 
0     2455260.853      19.472             0.150
1     2455260.900      19.445             0.126
2     2455261.792      19.484             0.168
3     2455262.830      19.157             0.261
4     2455264.814      19.376             0.150
...           ...         ...               ...
6096  2457686.478      19.063             0.176
6097  2457689.480      19.178             0.128
6098  2457690.475      19.386             0.171
6099  2457690.480      19.092             0.112
6100  2457691.476      19.191             0.122

我试图将每个数字分别打印在上面,在数据框中搜索它,并绘制数字和两侧的10个图。

我的最终结果应该一次打印60张左右的图形

任何建议或帮助将不胜感激。

编辑:

这里是数据集的链接,可以下载。

[1] https://spaces.hightail.com/space/BZuCAsY4nO

1 个答案:

答案 0 :(得分:1)

绘制多个图形的最佳方法是使用plt.subplots()。这很容易。数据创建正确。

import pandas as pd
import numpy as np
import random
times = pd.date_range('2000-01-01', freq='1d', periods=6000)
mag = np.random.randint(0,20, (6000,))
df = pd.DataFrame({'Time':times, 'Magnitude':mag})

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'].dt.date, rotation=45)

enter image description here

更新: 我使用已发布的数据运行了以上代码。图形已成功显示。

enter image description here

相关问题