Sammy
Sammy

Reputation: 15

Why isn't my data plotting to my pcolor plot?

I am trying to feed my csv data to a pcolor or pcolormesh plot and for some reason it isn't plotting. I don't get any errors, so I am not sure why it wouldn't be plotting. The Xaxis, Yaxis, and color bar scale all appear correct. My 'Intensity' data set is values between 0 and 1. I am new to python so any guidance will be extremely helpful.

import matplotlib.pyplot as plt
import pandas as pd
df=pd.read_csv('mycsv3.csv')
a=df['X'].values
b=df['Y'].values
c=df['Intensity'].values
cb=plt.pcolor([a],[b],[c],cmap=plt.cm.jet)
plt.title('Pupil Image')
plt.colorbar()
plt.show()

I tried to reshape my Intensity data to X and Y by adding an extra line: c =df['Intensity'].values.reshape(a,b) but got a "TypeError: only integer scalar arrays can be converted to a scalar index"

enter image description here enter image description here

Upvotes: 0

Views: 606

Answers (1)

Charles
Charles

Reputation: 104

I asked about working with data that has a similar structure for a similar goal earlier today, I would suggest that you could follow the same method.

Edit: I have changed the solution a bit, I created a small table of data for demonstration as

-1 -1 .2
-1  0 .5
-1  1 .9
-1  2 .7
0  -1  0
0   0 .8
0   1 .1
0   2 .9
1  -1 .5
1   0 .4
1   1 .6
1   2 .7

Solution

import matplotlib.pyplot as plt
import pandas as pd

header = ['X', 'Y', 'Intensity']
df_demo = pd.read_csv('democsv.csv')
df_demo.columns = header

df_pivot = df_demo.pivot(columns='X', index = 'Y', values='Intensity')
plt.imshow(df_pivot, extent =[-1, 1, -1, 2], origin = 'lower', cmap=plt.cm.jet)
plt.colorbar()

Output

Output

A weakness of my solution is that it also changes to plotting via imshow. This assumes that the sampling rate is equal. Also, note that I flipped the origin to match to more closely match this data as an example of how to do that.

Upvotes: 1

Related Questions