Home > Mobile >  How to plot line plots from a wide form dataframe
How to plot line plots from a wide form dataframe

Time:10-15

I can't seem to solve this for the life of me. I figure I need to do some sort of data sorting/display it differently in order to plot the graph but I'm not sure how. I have tried transposing the data set but that doesn't seem to do the trick either.

This is my data after slicing and I need to plot wavelength values as x axis vs the reflectance values as y1, y2, y3, y4 and y5

CodePudding user response:

For each graph you need two arrays or lists x and y.

Since x values are the same for every graph you can reuse them. You could get them from the keys of your DataFrame (assuming they are integers) like this:

x = [key for key in df.keys() if type(key) == int]

Next you need the y values for each graph. You can iterate the rows of a DataFrame with df.iterrows():

   fig, ax = plt.subplots()    # create figure and axes
   for index, row in data1[x].iterrows(): 
        ax.plot(x, row)
   plt.show()

data1[x] returns columns that are in x

iterrows()returns tuple of index and row. Row is of type pandas.Series

CodePudding user response:

  1. After sampling the dataframe, change the index values to the desired format of y1 to yn 1
  2. Select the desired columns with enter image description here

  • Related