Raivat Singhania
Raivat Singhania

Reputation: 15

Plot only plots all the way on the left, and shows black bars instead of x-axes labels

I have this plot that is generated by reading a CSV file, for some reason, when plotted it does this very strange thing and squishes the plot all the way to the left of the plot, and replaces the xlabels with black bars. I have included an image of the plot to show you what I mean, as well as the code being used to plot it. Any help would be really appreciated.

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

Plot

Upvotes: 1

Views: 90

Answers (1)

Mykola Semenov
Mykola Semenov

Reputation: 802

df_exp['Energy'].dtype is np.object, so it can't be interpreted as float. Solution is to convert its type to float:

df_exp['Energy'] = df_exp['Energy'].astype(np.float, copy=False)

Upvotes: 1

Related Questions