0

I would like to change the start and end month on my histogram so I can see the bars followed: November, December, January, February an March.

The idea is start the hist.plot from month 7 and end in 6 (for example).

My code is something like this:

d = {'days': [7 , 33 , 50 , 51 , 53 , 71 , 84 , 85 ,324 ,343 ,344 ,345 ,357], 
     'month': [ 1, 2, 2, 2, 2, 3, 3, 3,11,12,12,12,12]}
df_example = pd.DataFrame(data=d)

Then the function I'm using:

def histogram_ssw(variable1, variable2, title):
    
    fig, ax = plt.subplots(1,1,figsize=(20,7))
    fig.suptitle('Histogram' + title,fontsize=22,weight='bold')
    
    mean = variable2.mean()
    std  = variable2.std()
    count1,bins1,ignored1 = ax.hist(variable1[~np.isnan(variable1)],bins=25,density='True')#,label='empirica')
    ax.set_title('whatever');
    ax.set_xlabel('days') 
    ax.set_ylabel('frec')
    ylim0 = ax.get_ylim()
    x1 = np.linspace(bins1.min(),bins1.max(),100)
    N = nor(np.nanmean(variable2),variable2.std(),x1)
    x_formatter = dates.DateFormatter('%m-%d')
    #ax.xaxis.set_major_formatter(x_formatter)
    ax.xaxis.set_major_locator(dates.DayLocator(interval=1))
    plt.gcf().autofmt_xdate(rotation=60)
    #ax.set_xticklabels(df_niño3EP["M-D"].tolist(),rotation = 60)
    #ax.tick_params(axis="both", direction="in", pad=15)
    #ax.get_xticks(df_niño3EP["M-D"])
    #mmax = np.max(ylim0[1])
    #ax.set_ylim([0,mmax])
    ax.grid()
    
    plt.show()

We execute the function for my df:

histogram_ex(df_example['months'], df_ejemplo['days'], 'pff')

So it returns:

histogram

As you can see they're separated and I want it centered...

1 Answers1

0

try this

def histogram_ssw(variable1, variable2, title):
    
    fig, ax = plt.subplots(1,1,figsize=(20,7))
    fig.suptitle('Histogram' + title,fontsize=22,weight='bold')
    
    mean = variable2.mean()
    std  = variable2.std()
# CHANGE WAS MADE HERE 

    count1,bins1,ignored1 = ax.hist(variable1[~np.isnan(variable1)],bins=25,density='True',bottom='scalar'),label='empirica')

    ax.set_title('whatever');
    ax.set_xlabel('days') 
    ax.set_ylabel('frec')
    ylim0 = ax.get_ylim()
    x1 = np.linspace(bins1.min(),bins1.max(),100)
    N = nor(np.nanmean(variable2),variable2.std(),x1)
    x_formatter = dates.DateFormatter('%m-%d')
    #ax.xaxis.set_major_formatter(x_formatter)
    ax.xaxis.set_major_locator(dates.DayLocator(interval=1))
    plt.gcf().autofmt_xdate(rotation=60)
    #ax.set_xticklabels(df_niño3EP["M-D"].tolist(),rotation = 60)
    #ax.tick_params(axis="both", direction="in", pad=15)
    #ax.get_xticks(df_niño3EP["M-D"])
    #mmax = np.max(ylim0[1])
    #ax.set_ylim([0,mmax])
    ax.grid()
    
    plt.show()

where i added an additional property bottom to your ax.hist() constructor.

i haven't tried it so lmk but more can be found here (docs)

dbakr
  • 217
  • 1
  • 10
  • Hey, thanks for your quick response. Apparentyl doesn't work bc: ConversionError: Failed to convert value(s) to axis units: 'scalar' – Azar_y_Kaos May 31 '22 at 19:41
  • okay try calling this before `plt.show()`: `locs, labels = xticks() # Get the current locations and labels` then `print(locs, labels)` and see what it says the ticks are currently set to – dbakr May 31 '22 at 22:12
  • @Azar_y_Kaos were you able to try getting the `xticks()` information? – dbakr Jun 02 '22 at 13:53