all_xs = all_ys = range(0, 1001) # get all discrete xs and ys values (on an integer basis)
missing_xs = list(set(all_xs) - set(df['x']*10)) #find all missing xs
missing_ys = list(set(all_ys) - set(df['y']*10)) #find all missing ys
dummy_df = pd.DataFrame(0, index=missing_ys, columns=missing_xs) #construct a dummy dataframe with those xs and ys (filled with zeros)
df2.index *= 10 #convert your indexes and columns of the previous df2 to integers
df2.columns *= 10
df3 = df2.append(dummy_df).fillna(0) #append the dataframes, which will create the missing columns and index and fill all empty values with zeros
df3.sort_index(inplace=True, ascending=False) #resort the index (ie ys values)
df3.sort_index(axis=1, inplace=True, ascending=True) #resort the columns (ie xs values)
print(df3.shape) #make sure the shape is a square array
array = df3.values #extract the numpy array
可以将列数据转换为numpy数组,并将其重塑为所需的尺寸:
考虑到您已经将x和y作为列,我将像这样使用pivot_table:
pivot_表的美妙之处在于,它将对x/y值进行排序
但是要小心,你的y值会上升(从上到下),这与你在图片中想要的相反。您必须以反向模式对索引重新排序(因此使用sort\u index命令)
这里还有一个假设:你的x和y的值是线性的,或者你不想画任何线性的东西。事实上,您的数据样本不是(例如,您的第一个离散y值是3.5、4、4.1);在网格格式中,您不能将这种坐标用于任何对象
鉴于您的数据坐标都是浮点数,最多只有一个小数点,您可以通过以下方式完成数据:
相关问题 更多 >
编程相关推荐