即使刻度线仍然存在,x轴标签也没有显示

时间:2019-11-12 16:17:14

标签: python

我在一个图的Gridspec上绘制了多个图表,但是无论出于何种原因,顶部的条形图都隐藏了它们的x轴标签。

图片(https://drive.google.com/file/d/1yAqvlUCt80CSGnAwG5nOc6a7M3363EPe/view?usp=sharing

我已经尽我所能并通过谷歌搜索尝试了一切,甚至使用plt.setp(ax.get_xticklabels(), visible=True)

下面是我的代码:

def nfr_lsr(d):
    d['%nfr'] = d['non_fulfilled_order'] / (d['non_fulfilled_order'] + d['net_order']) * 100
    d['%lsr'] = d['total_late_shipment'] / d['shipped_order'] * 100


def combo_charts(data, period, line_fact: str, bar_color: str, line_color: str, ax, limit=0):
    non_fulfilled_d = data.groupby(period)['verified_order', 'non_fulfilled_order', 'net_order', 
                                                 'shipped_order', 'total_late_shipment'].sum()
    nfr_lsr(non_fulfilled_d)

    if limit != 0:
        non_fulfilled_d = non_fulfilled_d.iloc[-(limit+1):-1, :]

    if period == 'created_month':
        non_fulfilled_d = non_fulfilled_d.iloc[:-1, :]
        ax.set_title('Monthly {}'.format(line_fact))
    else: ax.set_title('Weekly {}'.format(line_fact))

    if line_fact == '%nfr':
        bar_fact = 'non_fulfilled_order'
    else: bar_fact = 'total_late_shipment'

    # bar chart 
    x_labels = non_fulfilled_d.index.values.tolist() 
    bar_h = non_fulfilled_d[bar_fact].values.tolist() 
    bar_plot = ax.bar(x=x_labels, height=bar_h, color=bar_color, label=bar_fact)
    ax.tick_params(axis='y', labelcolor=bar_color)
    ax.ticklabel_format(axis='y', style='sci', scilimits=(5, 5))
    plt.setp(ax.get_xticklabels(), visible=True) # I tried this 

    # line chart
    ax1 = ax.twinx()
    line_x = non_fulfilled_d[line_fact].values.tolist() 
    line_y = non_fulfilled_d[line_fact].index.values.tolist() 
    line_plot = ax1.plot(line_y, line_x, 'o--', color=line_color, label=line_fact)
    ax1.tick_params(axis='y', labelcolor=line_color)
    for yx in zip(line_y, line_x):
        ax1.annotate('{:.1f}'.format(yx[1]),
                    xy=(yx[0], yx[1] + 0.25),
                    ha='center', va='bottom',
                    color=line_color)

    ax1.yaxis.set_visible(False)
    ax.set_ylim(bottom=0, top=max(bar_h)*1.2)
    ax1.set_ylim(bottom=0.00, top=10.00)

    # add legends 
    blegend = mpatches.Patch(color=bar_color, label=bar_fact)
    llegend = mlines.Line2D([], [], color=line_color, marker='o', label=line_fact)
    ax.legend(handles=[blegend, llegend], loc=0)


def growth_chart(d, period, title, ax):
    chg_order = d.groupby(period)['verified_order', 'non_fulfilled_order', 'total_late_shipment'].sum()
    chg_order = chg_order.pct_change() * 100.00
    chg_order.rename(mapper={'verified_order': 'verified',
                            'non_fulfilled_order': 'nf',
                            'total_late_shipment': 'late'}, axis=1, inplace=True)
    chg_plot = chg_order.iloc[-2, :].plot(kind='barh', ax=ax, title=title, width=0.5,
                                             color=['#F2B705' ,'#05DBF2', '#F27405'])

    ax.legend(loc='upper right')
    ax.axvline(x=0, color='grey', linestyle='--', linewidth=0.25)
    plt.yticks([1, 2, 3], chg_order.columns.tolist(), rotation=90)

    # annotating 
    for rect in chg_plot.patches:
        w, h = rect.get_width(), rect.get_height()
        x, y = rect.get_xy() 
        ax.text(x + w/2, 
                y + h/2, 
                '{:.1f}'.format(w),
                ha='center', va='center')


# plotting
fig = plt.figure(figsize=(15, 7.5), dpi=300)
plotgrid = gridspec.GridSpec(2, 6)

ax1 = fig.add_subplot(plotgrid[0, 0:2])
ax2 = fig.add_subplot(plotgrid[0, 2:4])
ax3 = fig.add_subplot(plotgrid[0:, 4])
ax5 = fig.add_subplot(plotgrid[1, 0:2])
ax6 = fig.add_subplot(plotgrid[1, 2:4])
ax8 = fig.add_subplot(plotgrid[0:, 5])
plotgrid.tight_layout(fig)

# combo charts
combo_charts(d, 'created_month', '%nfr', '#CBF9FE', '#036D79', ax1)
combo_charts(d, 'created_week', '%nfr', '#CBF9FE', '#036D79', ax2, 4)
combo_charts(d, 'created_month', '%lsr', '#FEE3CB', '#793A03', ax5)
combo_charts(d, 'created_week', '%lsr', '#FEE3CB', '#793A03', ax6, 4)


# growth charts 
growth_chart(d, 'created_month', '%Chg MoM', ax3)
growth_chart(d, 'created_week', '%Chg WoW', ax8)

如何“打开”顶部条形图的x轴标签?

0 个答案:

没有答案