我有一个通过读取CSV文件生成的图,出于某种原因,当它被绘制时,它做了一件非常奇怪的事情,把图一直压到了图的左边,并用黑条代替了xlabels。我已经包含了一个绘图的图像,以向您展示我的意思,以及用于绘制它的代码。任何帮助都将不胜感激。
def plot_exp(self, filename): # Plots what is selected using expButton, using the filename from exp_clicked
self.axes.set_xlim(200, 3000) # Sets the axes limits
self.axes.set_xlabel('Energy (eV)') # Sets the x label
self.axes.set_ylabel('Intensity (a.u.)') # Sets the y label
df = pd.read_csv(filename, sep='\t') # Reads the .csv file with appropriate separator
df_exp = df[df.columns[::2]] # Skips every other column in the .csv file as each is exported twice
df_exp = df_exp.drop(df_exp.columns[1], axis=1) # Drops the first column which is irrelevant
df_exp = df_exp.drop(df_exp.columns[2:7], axis=1) # Drops columns 2-7 which are also irrelevant
df_exp.columns = ['Energy', 'Intensity'] # Renames columns
df_exp = df_exp[df_exp['Intensity'] > 0] # Only reads data points which are greater than 0
df_exp['Energy'] = df_exp['Energy'].str.replace(',', '') # Replaces the comma separator with nothing
df_exp.to_csv('/Volumes/GoogleDrive/My Drive/MAT 395/Project/Exported Data/Experimental_Plot.csv', index=False)
# Exports plotted data to .csv
self.axes.plot(df_exp['Energy'], df_exp['Intensity']) # Plots the experimental data file
self.draw() # Draws the plot onto the canvas

发布于 2020-07-28 20:59:07
df_exp['Energy'].dtype是np.object,所以不能解释为float。解决方案是将其类型转换为float
df_exp['Energy'] = df_exp['Energy'].astype(np.float, copy=False)https://stackoverflow.com/questions/63134029
复制相似问题