Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

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."
I have provided the code that I've tried. Any suggestion is much appreciated.

Here is the link to my sampled data.
https://gofile.io/d/U4NT0e[^]

What I have tried:

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()
Posted
Updated 16-Sep-20 21:29pm
v3
Comments
Richard MacCutchan 16-Sep-20 12:36pm    
When you receive an error message, please add the complete text to your question, and indicate which line of code caused it to occur
Mahdi Hasanzadeh 16-Sep-20 13:16pm    
hist,xedges,yedges=np.histogram2d(df_pe, df_pa, bins=(xedge,yedge), normed=True, weights=weight)
File "D:\Anaconda\lib\site-packages\numpy\lib\twodim_base.py", line 655, in histogram2d
hist, edges = histogramdd([x, y], bins, range, normed, weights)
File "D:\Anaconda\lib\site-packages\numpy\lib\function_base.py", line 917, in histogramdd
'The dimension of bins must be equal to the dimension of the '
ValueError: The dimension of bins must be
equal to the dimension of the sample x.
Mahdi Hasanzadeh 18-Sep-20 15:49pm    
Thanks for your kindly response and answer on my question.
[no name] 16-Sep-20 12:50pm    
Sounds like your Csv "column count / indexing" might be off.
Mahdi Hasanzadeh 16-Sep-20 13:17pm    
Thanks for the response. what do you mean though? I print out the df1 and df2. They looks right though.

1 solution

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

# ...

hist,xedges,yedges=np.histogram2d(df1, df2, bins=(xedge,yedge), normed=True, weights=weight)
fxyz += hist.T

You are calling to_string() on variables which are already strings, read from the text file. You then pass these strings to np.histogram2d which expects two arrays in the firest two parameters.

See pandas.read_csv — pandas 1.1.2 documentation[^] and numpy.histogram2d — NumPy v1.19 Manual[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900