有两个x- is阵列按升序排列,y-混沌。如何以升序显示Y,而保存(x,y)
import numpy as np
import matplotlib.pyplot as plt
x=[]
with open('D:\Programming\Contest\stepperf.txt','r') as a:
for line in a:
x.append(line.strip())
x =np.array(x)
y=[]
with open('D:\Programming\Contest\Hall.txt','r') as b:
for line in b:
y.append(line.strip())
y = np.array(y)
fig, ax = plt.subplots(figsize=(8, 6))
plt.grid()
plt.plot(x,y)
plt.show()
发布于 2020-11-22 13:46:34
熊猫是将数组保持为表的一种很好的方法,并为您的问题提供了一个优雅的解决方案:
import pandas as pd
df = pd.DataFrame({'x': x, 'y': y})
我们将您的x
和y
存储到DataFrame,df
中,其中有两列-- x
和y
。
访问数据的两个选项:
df['x']
和df['y']
是Pandas的系列对象。df['x'].values
和df['y'].values
又回到了numpy数组。使用值对其进行排序
df_sorted_by_y = df.sort_values(by='y', ascending=True)
返回到numpy数组:
x_sorted = df_sorted_by_y['x'].values
y_sorted = df_sorted_by_y['y'].values
编辑:在最后一段被阻塞的代码中,将df
更改为df_sorted_by_y
https://stackoverflow.com/questions/64954795
复制相似问题