admin管理员组

文章数量:1432643

I have following function to produce seaborn bar charts.

def create_bar_chart(data,
                     numeric_col,
                     category_col,
                     group_col=None,
                     x_min=0,
                     x_max=100,
                     fig_width_cm=12,
                     fig_heigth_cm=8):

    fig, ax = plt.subplots(figsize=(cm*fig_width_cm, cm*fig_heigth_cm))
    palette= {"Portfolio": "#00915A", "Benchmark": "#B3B3B3"}
    if group_col is None:
        group_col = category_col
    sns.barplot(data, x=numeric_col, y=category_col, hue=group_col, legend=False, palette=palette, width=0.4)
    sns.despine(offset=10, trim=True)
    ax.set_xlim(x_min, x_max)
    ax.yaxis.grid(False) # Hide the horizontal gridlines
    ax.xaxis.grid(True) # Show the vertical gridlines

    for container in ax.containers:
        ax.bar_label(container, fmt='{:.2f}', fontsize=8)

    return fig

I want the x-Axis to have a value range between e.g. 0-100 and also have vertical gridlines for this range. Therefore, I am using ax.set_xlim() However, with the current setting the axis labels and vertical gridlines are determined by the maximum x value of the data... only the chart grid size is impacted by ax.set_xlim... A screenshot to illustrate:

Do you know what I am missing? Thanks

本文标签: matplotlibPython Seabornsetxlimaxis labels do not appear on axisStack Overflow