无法对我的数据使用 plt.subplots()

Not able to use plt.subplots() for my data

我正在使用 Lombscargle 函数输出我作为输入传递的信号的功率谱,我能够一个接一个地绘制这些图,但手头的任务是使用子图以一种方式绘制这些图有 5 行,4 列。

信号示例为:

signal = [ '254.24', '254.32', '254.4', '254.84', '254.24', '254.28', '254.84', '253.56', '253.76', '253.32', '253.88', '253.72', '253.92', '251.56', '253.04', '244.72', '243.84', '246.08', '245.84', '249.0', '250.08', '248.2', '253.12', '253.2', '253.48', '253.88', '253.12', '253.4', '253.4']

from scipy.signal import lombscargle 

def LSP_scipy(signal):
    start_ang_freq = 2 * np.pi * (60/60)
    end_ang_freq = 2 * np.pi * (240/60)
    SAMPLES = 5000
    SAMPLE_SPACING = 1/15 

    t = np.linspace(0,len(signal)*SAMPLE_SPACING,len(signal))
    period_freq = np.linspace(start_ang_freq,end_ang_freq,SAMPLES)
    
    modified_signal_axis = []
    modified_time_axis = []
    for count,value in enumerate(signal):
        if value != 'None':
            modified_signal_axis.append(float(value))
            modified_time_axis.append(t[count])
        prog = lombscargle(modified_time_axis, modified_signal_axis, period_freq, normalize=False, precenter = True)
    fig, axes = plt.subplots()
    ax.plot(period_freq,prog)

如何以矩阵格式绘制这些图形?

尝试循环方法,

您可以使用 for 循环并遍历子图。显示了一个非常简单的示例 below.The subplots 方法创建图形以及子图并存储在 ax 数组中。

import matplotlib.pyplot as plt

x = np.linspace(0, 10)
y = range(10)


fig, ax = plt.subplots(nrows=2, ncols=2)

for row in ax:
   for col in row:
      col.plot(x, y)


plt.show()

# or you can also do

for in range(2):         # row=0, col=0
  for j in range(2):     # row=0, col=1
     ax[i, j].plot(x,y)  # row=1, col=0
                         # row=1, col=1

然后一个想法是将信号放入一个 shape=(20,1) 的数组中,其中每一行对应于信号幅度或其他一些可测量的量。然后你可以做如下(检查输出只保留行直到 plt.text 你会明白的)。

 for i in range(1, 21):
    plt.subplot(5, 4, i)
    plt.text(0.5, 0.5, str((5, 4, i)),
         fontsize=18, ha='center')
    # Call the function here...get the value of period_freq and prog
    period_freq,prog = LSP_scipy(signal[i])
    plt.plot(period_freq, prog) 
  • 查看内联注释以添加和展平子图。
  • 这是从副本的 中压平 axes array 的实现。
from scipy.signal import lombscargle
from matplotlib.ticker import FormatStrFormatter
import numpy as np
import matplotlib.pyplot as plt

def LSP_scipy(signal):
    start_ang_freq = 2 * np.pi * (60/60)
    end_ang_freq = 2 * np.pi * (240/60)
    SAMPLES = 5000
    SAMPLE_SPACING = 1/15 

    t = np.linspace(0, len(signal)*SAMPLE_SPACING, len(signal))
    period_freq = np.linspace(start_ang_freq, end_ang_freq, SAMPLES)
    
    modified_signal_axis = []
    modified_time_axis = []
    
    # create the figure and subplots
    fig, axes = plt.subplots(5, 6, figsize=(20, 9), sharex=False, sharey=False)
    
    # flatten the array
    axes = axes.ravel()
    
    for count, value in enumerate(signal):
        if value != 'None':
            modified_signal_axis.append(float(value))
            modified_time_axis.append(t[count])
        prog = lombscargle(modified_time_axis, modified_signal_axis, period_freq, normalize=False, precenter=True)
        
        # plot
        axes[count].plot(period_freq, prog)
        
        # format the axes
        axes[count].set(title=value)
        # some plot have an exponential offset on the yaxis, this turns it off
        axes[count].ticklabel_format(useOffset=False)
        # some yaxis values are long floats, this formats them to 3 decimal places
        axes[count].yaxis.set_major_formatter(FormatStrFormatter('%.3f'))
        
    # format the figure
    fig.tight_layout()


signal = [ '254.24', '254.32', '254.4', '254.84', '254.24', '254.28', '254.84', '253.56', '253.76', '253.32', '253.88', '253.72', '253.92', '251.56', '253.04', '244.72', '243.84', '246.08', '245.84', '249.0', '250.08', '248.2', '253.12', '253.2', '253.48', '253.88', '253.12', '253.4', '253.4']
LSP_scipy(signal[:20])  # as per comment, only first 20