I have the following code.
I am trying to loop through columns of a dataframe (newerdf) and plot a histogram for each one.
I am then saving each plot as a .png file on my desktop.
However, the following code gives me the error: 'numpy.ndarray' object has no attribute 'tick_params'.
I would be so grateful for a helping hand!
listedvariables = ['distance','age']
for i in range(0,len(listedvariables)):
x = newerdf[[listedvariables[i]]].hist(figsize=(50,50))
x.tick_params(axis='x',labelsize=60)
x.tick_params(axis='y',labelsize=60)
x.set_xlabel(var,fontsize=70,labelpad=30,weight='bold')
x.set_ylabel('Number of participants',fontsize=70,labelpad=30,weight='bold')
x.set_title(var,fontsize=70,pad=30,weight='bold')
dir_name = "/Users/macbook/Desktop/UCL PhD Work/"
plt.rcParams["savefig.directory"] = os.chdir(os.path.dirname(dir_name))
plt.savefig(var ' ' 'histogram')
plt.show()
The first 10 rows of newerdf['age'] look like this:
0 21.0
1 24.0
2 47.0
3 32.0
5 29.0
6 29.0
7 22.0
8 23.0
9 32.0
10 22.0
CodePudding user response:
The DataFrame.hist()
function returns, according to its documentation, a matplotlib axes or a numpy array of them, if your dataframe has more then one column.
This function calls matplotlib.pyplot.hist(), on each series in the DataFrame, resulting in one histogram per column.
Thus in this line,
x = newerdf[[listedvariables[i]]].hist(figsize=(50,50))
x
is set to a numpy array, and numpy arrays have no attribute tick_params
.
to fix this, loop over the values in x as:
for i in range(0,len(listedvariables)):
x = newerdf[[listedvariables[i]]].hist(figsize=(50,50))
for hist in x:
# remainder of your code
CodePudding user response:
The following code works.
I added '.plot' between the df 'newer[df]' and 'hist'.
listedvariables = ['distance','age']
for i in range(0,len(listedvariables)):
x = newerdf[[listedvariables[i]]].plot.hist(figsize=(50,50))
x.tick_params(axis='x',labelsize=60)
x.tick_params(axis='y',labelsize=60)
x.set_xlabel(listedvariables[i],fontsize=70,labelpad=30,weight='bold')
x.set_ylabel('Number of participants',fontsize=70,labelpad=30,weight='bold')
x.set_title(listedvariables[i],fontsize=70,pad=30,weight='bold')
dir_name = "/Users/macbook/Desktop/UCL PhD Work/"
plt.rcParams["savefig.directory"] = os.chdir(os.path.dirname(dir_name))
plt.savefig(listedvariables[i] ' ' 'histogram')
plt.show()