簡體   English   中英

Matplotlib 欄 plot 自動標簽子圖 - 名稱“axs”無法識別

[英]Matplotlib bar plot autolabel subplots - name 'axs' isn't recognized

I define a function to automatically create three bar plots one above the other (so with subplots) from one single dataframe and inside this function I call another function to label each bar.

這是酒吧 plot function (確實簡化但問題是先驗相同):

> def bar_plot_one_category_by_year(df_name,category,df_years=['2019','2020']):
>     nbr_of_years=len(df_years)
>     sorts_of_representation_list=['number','sum','median']
>     
>     # Dataframes list
>     df_list=[]
>     for i in range(nbr_of_years):
>         df_list=df_list.append(globals()[df_name+df_years])
>     
>     # Create a figure with subplots
>     fig, axs = plt.subplots(nrows=nrows, ncols=1,figsize=(18,18))
>     
>     # Bars setup
>     bar_width=0.4
>     bar_width_shift_factor=1.1
>     bar_shift=bar_width*bar_width_shift_factor # Shift values of bars from the center of ticks
>     bar_shift_list=[bar_shift*i for i in [i-nbr_of_years/2 for i in range(nbr_of_years)]]
>     colors=['r','b']
>     
>     # xticks
>     nbr_of_xticks=len(df_list[0])
>     xticks_pos=np.arange(nbr_of_xticks) # ticks position
>     if nbr_of_years%2==0:
>         plt.setp(axs, xticks=xticks_pos-bar_shift/2)
>     else:
>         plt.setp(axs, xticks=xticks_pos)
>     
>     # Plot 1 - by number
>     list_number = []
>     for i in range(0,nbr_of_years):
>         list_number.append(list(df_list[i]['Number']))
>         plot_number=axs[0].bar(xticks_pos + bar_shift_list[i], height = list_number[i], width = bar_width, color = colors[i], align='center', label=df_years[i])
>         autolabel(plot_number,axs_nbr=0)
>     axs[0].set_title('Number',fontdict=fontdict)
>     axs[0].legend()
> 
>     # Plot 2 - by sum
>     list_sum = []
>     for i in range(0,nbr_of_years):
>         list_sum.append(list(df_list[i]['Total_paid_amount']))
>         plot_sum=axs[1].bar(xticks_pos + bar_shift_list[i], height = list_sum[i], width = bar_width, color = colors[i], align='center',
> label=df_years[i])
>         autolabel(plot_sum,axs_nbr=1)
>     axs[1].set_title('Total paid amount')
>     axs[1].legend()
> 
>     # Plot 3 - by median
>     list_median = []
>     for i in range(0,nbr_of_years):
>         list_median.append(list(df_list[i]['Median_of_paid_amounts']))
>         plot_median=axs[2].bar(xticks_pos + bar_shift_list[i], height = list_median[i], color = colors[i],align='center', label=df_years[i])
>         autolabel(plot_median,axs_nbr=2)
>     axs[2].set_title('Median of paid amounts')
>     axs[2].legend()

這是自動標簽 function:

>     def autolabel(plot_name,fontsize = 8,axs_nbr=0):
>         for p in plot_name.patches:
>             height = p.get_height()
>             if (height != 0):             # Without this condition, we could have a Value message error
>                 axs[axs_nbr].text(p.get_x() + p.get_width()/2., height,'{:,}'.format(int(height)).replace(',',' '),ha='center',
> va='bottom')

問題:我收到錯誤"NameError: name 'axs' is not defined" 這個錯誤是在(希望)解決了其他問題之后出現的,例如關於最后一個 plot 的“IndexError:index 2 is out of the bounds for axis 0 with size 2”。 已經好幾個小時了,我仍然無法打印我的標簽。

哦,我發布答案就在我腦海中的問題就足夠了:

在 fig,axs 聲明的下方添加globals()['axs'] = axs :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM