matplotlib 如何从不同的文本文件中绘制垂直堆叠图?

wljmcqd8  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(142)

我有5个txt文件,其中包含的数据给予我的影响,增加热量对我的样品,我想把它们绘制在一个垂直堆叠图,其中最后的数字是5个垂直堆叠图共享相同的X轴,每条线在一个单独的,以揭示它们之间的差异。
我写了这段代码:

import glob
import pandas as pd
import matplotlib.axes._axes as axes
import matplotlib.pyplot as plt

input_files = glob.glob('01-input/RR_*.txt')
for file in input_files:
    data = pd.read_csv(file, header=None, delimiter="\t").values
    x = data[:,0]
    y = data[:,1]
    plt.subplot(2, 1, 1)
    plt.plot(x, y, linewidth=2, linestyle=':')
    plt.tight_layout()
    plt.xlabel('x-axis')
    plt.ylabel('y-axis')

但结果只有一个包含所有直线的图形:

我想得到下面的图表:

o2rvlv0m

o2rvlv0m1#

import matplotlib.pyplot as plt
import numpy as np

# just a dummy data
x = np.linspace(0, 2700, 50)
all_data = [np.sin(x), np.cos(x), x**0.3, x**0.4, x**0.5]

n = len(all_data)
n_rows = n
n_cols = 1
fig, ax = plt.subplots(n_rows, n_cols) # each element in "ax" is a axes

for i, y in enumerate(all_data):
    ax[i].plot(x, y, linewidth=2, linestyle=':')
    ax[i].set_ylabel('y-axis')
    # You can to use a list of y-labels. Example:
    # my_labels = ['y1', 'y2', 'y3', 'y4', 'y5']
    # ax[i].set_ylabel(my_labels[i])
    # The "my_labels" lenght must be "n" too

plt.xlabel('x-axis') # add xlabel at last axes
plt.tight_layout()

相关问题