换档直方图X轴杆的开始和结束(从7月到6月)

发布于 2025-02-03 07:11:21 字数 1598 浏览 5 评论 0原文

我想更改直方图的开始和结束月的开始和结束月,以便看到酒吧如下:11月,12月,1月,2月3月。

这个想法是从第7个月开始开始的hist.lot。以6结束(例如)。

我的代码是这样的:

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)

然后我正在使用的函数:

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()

我们执行DF的函数:

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

返回:

直方图

您可以看到它们是分开的,我希望它以中心为中心...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南街女流氓 2025-02-10 07:11:22

尝试此处

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()

,我将附加属性底部添加到您的ax.hist()构造函数中。

我没有尝试过,所以LMK,但是可以找到更多在这里 (文档)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文