problem in reading and plotting sampled data using historgram2d

87 views Asked by At

I have some data and want to read the 5th and 6th column so to plot the 2D histogram of it. I have written the following code but it fails. I would be appreciated if someone can help me in this process.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt 

path = r'D:\test_file\data\final.txt'
df = pd.read_csv( path , header = None, sep = '   ', dtype = float, engine = 'python')
df1 = df[4].to_string()
df2 = df[5].to_string()

lmin = -1
lmax = 1
nbins = 100    
xedge = np.linspace(lmin, lmax, nbins + 1)
yedge = np.linspace(lmin, lmax, nbins + 1)
fxyz = np.zeros((nbins,nbins))

weight = None
hist,xedges,yedges=np.histogram2d(df1, df2, bins=(xedge,yedge), normed=True, weights=weight)
fxyz += hist.T
extent = (xedges[0],xedges[-1],yedges[0],yedges[-1])  
with np.errstate(divide='ignore',invalid='ignore'):
    plt.figure(figsize=(10,6))
    images = plt.imshow(np.log10(fxyz),  origin='lower', extent=extent,cmap='jet')
    plt.ylim(0,0.7); plt.xlim(-0.7,0.7)
    plt.show()

I have provided the sampled data that I'm trying to plot using np.histogram2d.

0.43018E+03    0.15502E+03    0.16345E+03    0.88100E+02    0.81300E-01    0.45961E-01    0.93393E-01    0.11000E+02    0.60519E+02    0.19494E-02    0.87222E-02
0.43018E+03    0.15357E+03    0.16490E+03    0.88590E+02    0.18111E-01    0.50586E-01    0.53730E-01    0.13000E+02    0.19699E+02    0.29516E-03    0.28869E-02
0.42985E+03    0.15487E+03    0.16076E+03    0.86200E+02    0.31413E+00   -0.47104E-01    0.31764E+00    0.44000E+02    0.98528E+02    0.24046E-02    0.10090E+00
0.43218E+03    0.15269E+03    0.15991E+03    0.85087E+02    0.27966E+00   -0.24480E-01    0.28073E+00    0.48000E+02    0.95003E+02    0.23724E-02    0.78811E-01
0.43198E+03    0.15502E+03    0.15898E+03    0.86400E+02    0.21330E+00    0.14911E-01    0.21382E+00    0.58000E+02    0.86001E+02    0.24467E-02    0.45721E-01
0.43018E+03    0.15285E+03    0.16391E+03    0.88779E+02    0.12177E+00    0.42066E-01    0.12883E+00    0.60000E+02    0.70942E+02    0.23189E-02    0.16598E-01
0.42932E+03    0.15494E+03    0.15074E+03    0.80453E+02    0.16504E+00    0.59157E-01    0.17532E+00    0.96000E+02    0.70280E+02    0.19286E-02    0.30737E-01
0.43018E+03    0.15294E+03    0.15555E+03    0.84556E+02    0.68522E-01    0.73532E-01    0.10051E+00    0.98000E+02    0.42980E+02    0.10924E-02    0.10102E-01
0.42998E+03    0.15441E+03    0.15640E+03    0.84771E+02    0.81413E-01    0.65909E-01    0.10475E+00    0.11600E+03    0.51007E+02    0.14324E-02    0.10972E-01
0.43218E+03    0.15345E+03    0.16357E+03    0.88212E+02    0.21151E+00    0.10160E-01    0.21175E+00    0.12000E+03    0.87250E+02    0.25636E-02    0.44838E-01
0.43018E+03    0.15371E+03    0.16072E+03    0.87529E+02    0.11932E+00    0.50841E-01    0.12970E+00    0.12800E+03    0.66921E+02    0.21275E-02    0.16822E-01
0.43018E+03    0.15305E+03    0.16046E+03    0.85433E+02    0.76500E-01    0.60164E-01    0.97324E-01    0.14000E+03    0.51816E+02    0.14900E-02    0.94719E-02
0.43058E+03    0.15174E+03    0.16286E+03    0.88932E+02    0.20539E+00    0.71894E-02    0.20552E+00    0.14500E+03    0.87995E+02    0.25850E-02    0.42238E-01
0.42998E+03    0.15369E+03    0.16085E+03    0.87838E+02    0.11059E+00    0.46688E-01    0.12004E+00    0.19100E+03    0.67111E+02    0.21472E-02    0.14409E-01
0.43098E+03    0.15272E+03    0.15386E+03    0.82405E+02    0.29907E+00   -0.21818E-01    0.29986E+00    0.19700E+03    0.94173E+02    0.22350E-02    0.89917E-01
0.43225E+03    0.15447E+03    0.15624E+03    0.83131E+02    0.31363E+00   -0.42589E-01    0.31651E+00    0.20300E+03    0.97733E+02    0.22566E-02    0.10017E+00
0.43018E+03    0.15490E+03    0.16393E+03    0.89318E+02    0.12753E-01    0.56796E-01    0.58210E-01    0.20700E+03    0.12656E+02    0.12637E-03    0.33884E-02
0.43018E+03    0.15475E+03    0.15928E+03    0.86231E+02    0.53204E-01   -0.13231E+00    0.14260E+00    0.22900E+03    0.15809E+03    0.34108E-03    0.20335E-01
0.42906E+03    0.15422E+03    0.16079E+03    0.86668E+02    0.98950E-01    0.54275E-01    0.11286E+00    0.24400E+03    0.61255E+02    0.19037E-02    0.12737E-01
0.43018E+03    0.15275E+03    0.15731E+03    0.84636E+02    0.20413E+00    0.25174E-01    0.20568E+00    0.25500E+03    0.82970E+02    0.23254E-02    0.42302E-01
0.43018E+03    0.15507E+03    0.16462E+03    0.87910E+02    0.48585E-01    0.50105E-01    0.69792E-01    0.26200E+03    0.44117E+02    0.12466E-02    0.48710E-02
0.42912E+03    0.15243E+03    0.15654E+03    0.84454E+02    0.19921E+00    0.34896E-01    0.20224E+00    0.26800E+03    0.80064E+02    0.22779E-02    0.40903E-01
0.42998E+03    0.15204E+03    0.15919E+03    0.86962E+02    0.17219E+00    0.12718E+00    0.21407E+00    0.29800E+03    0.53550E+02    0.15980E-02    0.45826E-01
0.43132E+03    0.15362E+03    0.15621E+03    0.85371E+02    0.11943E+00    0.56797E-01    0.13225E+00    0.31100E+03    0.64566E+02    0.19514E-02    0.17491E-01
0.43085E+03    0.15329E+03    0.16482E+03    0.88291E+02    0.17100E+00    0.55323E-01    0.17972E+00    0.32700E+03    0.72072E+02    0.23369E-02    0.32300E-01
0.43018E+03    0.15537E+03    0.15573E+03    0.83989E+02    0.79878E-01    0.66928E-01    0.10421E+00    0.33900E+03    0.50041E+02    0.13740E-02    0.10860E-01
0.43178E+03    0.15418E+03    0.15631E+03    0.85402E+02    0.19419E+00    0.29949E-01    0.19648E+00    0.34700E+03    0.81233E+02    0.23416E-02    0.38605E-01
0.42932E+03    0.15283E+03    0.16053E+03    0.87613E+02    0.24661E+00   -0.11198E-01    0.24686E+00    0.36400E+03    0.92600E+02    0.25066E-02    0.60941E-01
0.43085E+03    0.15427E+03    0.15232E+03    0.81579E+02    0.20258E+00    0.39579E-01    0.20641E+00    0.37500E+03    0.78945E+02    0.21388E-02    0.42604E-01
0.43065E+03    0.15262E+03    0.15612E+03    0.84541E+02    0.25135E+00    0.77533E-04    0.25135E+00    0.40400E+03    0.89982E+02    0.23509E-02    0.63176E-01
0.43018E+03    0.15345E+03    0.16099E+03    0.87287E+02    0.72312E-01    0.58600E-01    0.93075E-01    0.41300E+03    0.50979E+02    0.15111E-02    0.86630E-02
0.43018E+03    0.15424E+03    0.15817E+03    0.84540E+02    0.11893E-01    0.68858E-01    0.69878E-01    0.44300E+03    0.97991E+01    0.68568E-04    0.48829E-02
0.43112E+03    0.15277E+03    0.15641E+03    0.84085E+02    0.19829E+00    0.13838E+00    0.24180E+00    0.44800E+03    0.55090E+02    0.15684E-02    0.58468E-01
0.43018E+03    0.15542E+03    0.16009E+03    0.86478E+02    0.15938E+00    0.40273E-01    0.16439E+00    0.45200E+03    0.75819E+02    0.23205E-02    0.27025E-01
0.43018E+03    0.15269E+03    0.15648E+03    0.85096E+02    0.89405E-02    0.72689E-01    0.73236E-01    0.45500E+03    0.70120E+01    0.35439E-04    0.53636E-02
0.43018E+03    0.15463E+03    0.16391E+03    0.89071E+02    0.97206E-01    0.46517E-01    0.10776E+00    0.46800E+03    0.64427E+02    0.21312E-02    0.11613E-01
0.43085E+03    0.15498E+03    0.16235E+03    0.88307E+02    0.17010E+00    0.30320E-01    0.17278E+00    0.47300E+03    0.79893E+02    0.24936E-02    0.29854E-01
0.42953E+03    0.15326E+03    0.16217E+03    0.87351E+02    0.25562E+00   -0.13319E-01    0.25597E+00    0.52100E+03    0.92983E+02    0.25088E-02    0.65519E-01
0.42978E+03    0.15505E+03    0.15256E+03    0.82348E+02    0.17569E+00    0.12443E+00    0.21529E+00    0.53200E+03    0.54691E+02    0.15029E-02    0.46349E-01
0.43018E+03    0.15369E+03    0.16159E+03    0.88210E+02    0.48787E-01    0.59771E-01    0.77154E-01    0.53700E+03    0.39223E+02    0.10213E-02    0.59528E-02
0.43018E+03    0.15288E+03    0.15642E+03    0.83645E+02    0.89082E-01    0.63869E-01    0.10961E+00    0.55900E+03    0.54361E+02    0.15278E-02    0.12015E-01
0.43018E+03    0.15413E+03    0.15104E+03    0.80875E+02    0.75548E-01    0.83346E-01    0.11249E+00    0.56900E+03    0.42190E+02    0.98766E-03    0.12654E-01
0.43178E+03    0.15389E+03    0.15891E+03    0.86886E+02    0.31884E+00   -0.55254E-01    0.32359E+00    0.57900E+03    0.99832E+02    0.24017E-02    0.10471E+00
0.42952E+03    0.15232E+03    0.16457E+03    0.89637E+02    0.18355E+00    0.16231E-01    0.18426E+00    0.58200E+03    0.84947E+02    0.26189E-02    0.33953E-01
0.43252E+03    0.15377E+03    0.15705E+03    0.83936E+02    0.19448E+00    0.29076E-01    0.19664E+00    0.60300E+03    0.81497E+02    0.22818E-02    0.38669E-01
0.42978E+03    0.15270E+03    0.16022E+03    0.85295E+02    0.69687E-01    0.61699E-01    0.93076E-01    0.61400E+03    0.48479E+02    0.13464E-02    0.86631E-02
0.43237E+03    0.15300E+03    0.15771E+03    0.84757E+02    0.21302E+00    0.20596E-01    0.21401E+00    0.62000E+03    0.84477E+02    0.23474E-02    0.45800E-01
0.43018E+03    0.15408E+03    0.16289E+03    0.87419E+02    0.13878E+00    0.34233E-01    0.14294E+00    0.62900E+03    0.76144E+02    0.23837E-02    0.20432E-01
0.43132E+03    0.15455E+03    0.16048E+03    0.86743E+02    0.15320E+00    0.98882E-01    0.18234E+00    0.63200E+03    0.57160E+02    0.17501E-02    0.33248E-01
0.43018E+03    0.15485E+03    0.16244E+03    0.87216E+02    0.10755E+00    0.48003E-01    0.11777E+00    0.63700E+03    0.65947E+02    0.21015E-02    0.13871E-01
0.42978E+03    0.15387E+03    0.16303E+03    0.88360E+02    0.13933E+00    0.39017E-01    0.14469E+00    0.64800E+03    0.74356E+02    0.23869E-02    0.20936E-01
0.42978E+03    0.15170E+03    0.16354E+03    0.89014E+02    0.80914E-01    0.50089E-01    0.95163E-01    0.65000E+03    0.58241E+02    0.18780E-02    0.90560E-02
0.43018E+03    0.15467E+03    0.15483E+03    0.84562E+02    0.16144E+00    0.52202E-01    0.16967E+00    0.68500E+03    0.72081E+02    0.21322E-02    0.28786E-01
0.43198E+03    0.15275E+03    0.15494E+03    0.82111E+02    0.20494E+00    0.32830E-01    0.20755E+00    0.69700E+03    0.80899E+02    0.21839E-02    0.43078E-01
0.42998E+03    0.15244E+03    0.15643E+03    0.83611E+02    0.58673E-01    0.68198E-01    0.89964E-01    0.70300E+03    0.40706E+02    0.98235E-03    0.80935E-02
0.43018E+03    0.15430E+03    0.16336E+03    0.88006E+02    0.16914E+00    0.29262E-01    0.17165E+00    0.71100E+03    0.80185E+02    0.24886E-02    0.29465E-01
0.43285E+03    0.15289E+03    0.15631E+03    0.84583E+02    0.18913E+00    0.37770E-01    0.19286E+00    0.72400E+03    0.78706E+02    0.22646E-02    0.37196E-01
0.43018E+03    0.15320E+03    0.16014E+03    0.86892E+02    0.84067E-01    0.60640E-01    0.10366E+00    0.72700E+03    0.54196E+02    0.16284E-02    0.10745E-01
0.43152E+03    0.15304E+03    0.16001E+03    0.86859E+02    0.13728E+00    0.43349E-01    0.14396E+00    0.73500E+03    0.72475E+02    0.22479E-02    0.20724E-01
0.42952E+03    0.15360E+03    0.16416E+03    0.88114E+02    0.13242E+00    0.35698E-01    0.13715E+00    0.73700E+03    0.74913E+02    0.23963E-02    0.18810E-01

There is a dimensional error message that I don't understand, something like, "The dimension of bins must be equal to the dimension of the sample x." Any suggestion is much appreciated.

One more question, how should I define the sep=' ' properly based on my sampled data? because the space between the float data might change due to some negative values.

1

There are 1 answers

0
Anonymous On BEST ANSWER

With the help of a good friend of mine, I could find the problem in my code. The problems lies in calling the "to_string()" on variables which are already strings.

 df1 = df[4].to_string() 
df2 = df[5].to_string()

should be replaced with df1 = df[4] & df2 = df[5].