我需要将多个x-y数据叠加在一个偏移量之上

问题描述

我希望绘图如图XRD results所示。我正在尝试编写python代码,该代码从特定目录的'.xy'文件扩展名输入所有数据。我得到两个重叠的地块。我想要这些图Overlapping data间的y偏移。

import matplotlib.pyplot as plt 
import os

px=[]
py=[]
for h in os.listdir(r'\\my directory')
    if h.endswith(".xy"):
        
        
        #print(m)
        x = []
        y = []
        f = open(h,"r")
        for i in f:
            row=i.split()
            x.append(row[0])
            y.append(row[1]) 
        
        for j in range(0,len(x)):
                x[j]=float(x[j])

        for j in range(0,len(y)):
                y[j]=float(y[j])
    
        px.append(x)
        py.append(y)
            
for i in range(len(x)):
    plt.plot(px[i],py[i])
    

我试图为所有y轴数据添加一个常数,但是它会根据偏移量更改图形的形状。我是python的新手。请忍受我。

解决方法

很难确定,但是看起来您想要这样的东西:

import matplotlib.pyplot as plt 
import os
import numpy as np

# Adjust the values in y_offsets as needed,this controls the spacing between plots
y_offsets = np.linspace(0,25,len(os.listdir(r'\\my directory')))

px=[]
py=[]
# Use zip here to make the data from each file offset by the specified amount
for h,y_off in zip(os.listdir(r'\\my directory'),y_offsets):
    if h.endswith(".xy"):
        
        
        #print(m)
        x = []
        y = []
        with open(h,"r") as f: #closes the file when finished
            for i in f:
                row=i.split()
                x.append(row[0])
                y.append(row[1]) 
        
        for j in range(0,len(x)):
                x[j] = float(x[j])

        for j in range(0,len(y)):
                y[j] = float(y[j])+y_off # add y_off here,to shift all the y positions
    
        px.append(x)
        py.append(y)
            
for i in range(len(x)):
    plt.plot(px[i],py[i])
,

我看不到你的问题

import numpy as np 
import matplotlib.pyplot as plt 
 
x = np.linspace(0,6.28,629) 
y = np.sin(x)*np.exp(-0.8*x)                                                      

for n in range(5): 
    plt.plot(x,y+n) 
plt.yticks([]) ; plt.show()                                                       

给我

enter image description here

符合预期。

能否请您详细解释出了什么问题,以便我们可能为您提供帮助?