我需要用偏移量将多个 x-y 数据堆叠在另一个之上
I need to stack the multiple x-y data one above the other with an offset
我要我的情节如图XRD results. I am trying to write a python code, which inputs all the data from '.xy' file extension of a particular directory. I am getting two plots overlapped over each other. I want an y-offset between these plots Overlapping data.
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()
给我
符合预期。
能否请您详细解释一下哪里出了问题,以便我们可能会帮助您?
我要我的情节如图XRD results. I am trying to write a python code, which inputs all the data from '.xy' file extension of a particular directory. I am getting two plots overlapped over each other. I want an y-offset between these plots Overlapping data.
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()
给我
符合预期。
能否请您详细解释一下哪里出了问题,以便我们可能会帮助您?