matplotlib 从带有2个标题的df/ excel-tabelle创建子图

mrzz3bfm  于 2023-11-22  发布在  其他
关注(0)|答案(2)|浏览(137)

从根本上说,代码是贯穿的;然而,它迭代了类别,只在所有4个子图中绘制了最后一个(D)。我不知道如何管理上下文,以适当地存储实验和类别的数据,然后将它们绘制在各自的子图中。

df = pd.read_excel("somedata.xlsx",  header=[0, 1])

experiments = list(range(1, 29))

fig, axes = plt.subplots(2, 2, figsize=(12, 8))
fig.suptitle('Zeitreihenanalyse für Experimente 1-28')

categories = df.columns.get_level_values(1).unique()

for category in categories:
    row, col = divmod(categories.get_loc(category), 2)
    ax = axes[row, col]

    for experiment in experiments:
        
        data = df.iloc[1:327, experiment].values
        sampling_interval = 30  # Minuten
        time = [t * sampling_interval for t in range(len(data))]

        ax.plot(time, data, label=f'Experiment {experiment}')

    ax.set_xlabel('time (Minutes)')
    ax.set_ylabel('Bioluminescence')
    # ax.legend()

plt.tight_layout()
plt.subplots_adjust(top=0.9)
plt.show()

字符串
我想画出实验1-28的数据(它在第一个标题中)。每个类别A,N,D,V(第二个标题)将有自己的子情节(两者都没有名称/titel btw。)。Y轴应显示每个实验的数据作为时间序列分析。有324个数据点与“30分钟内记录的采样间隔”。我很感激任何帮助,对这个东西还很陌生。thats hor the df looks like, the categories in the 2nd header repeat for each experiment
So the plots emerge but I can see that this are all the same data from categorie D

zy1mlcev

zy1mlcev1#

你是不是把事情弄得太复杂了?
将数据读入二维数组,在相应的子图中绘制每行。完成。


的数据

import matplotlib.pyplot as plt
import numpy as np

categories = ['A', 'B', 'C', 'D']
Nexp = 28
Ncat = 4
Npts = 329

# faking some data, ty for the real data
data = np.arange(Nexp*Npts, dtype=float).reshape(Nexp, Npts)

# fig, axes, reshape the axes in a dictionary indexed by category
fig, axes = plt.subplots(2, 2,
                         figsize=(10, 6),
                         layout='constrained')
axes = dict(zip(categories, axes.flatten()))

#plot the data
for exp, pts in enumerate(data):
    trial, index = divmod(exp, Ncat)
    category = categories[index]
    ax = axes[category]
    ax.plot(pts, label='Trial no. '+str(trial+1))
for category, ax in axes.items():
    ax.legend()
    ax.set_title('Category '+category)
fig.suptitle('Suptitle')
plt.show()

字符串

odopli94

odopli942#

我通过改变它找到了一个解决方案:

data = df.loc[1:327, (experiment, category)].values

字符串

相关问题