0

i created a mock dataset just to learn how to do plotting better but i am having a problem like this.enter image description here

As you can see the values on X axis are not on the same axis as the bars.

Here is my code

    veri = pd.read_excel("survey_1.xlsx") #
    fig, ax = plt.subplots(figsize=(96, 33))
    sns.barplot(x=xEkseni, y=yEkseni, data=veri, ci=None,palette='Set2', hue = neyeGore)
    ax = ax
    width_scale = 0.6
    secilen_ilce_partileri = (veri[xEkseni].unique())
    donecek_veri =  (set((secilen_ilce_partileri)))

    def operationSum(donecek_veri):
        deneme_doldur = []
        for element in donecek_veri:
            deneme_doldur.append(sum(veri[neyeGore] == element))
        return deneme_doldur
    for bars in ax.containers:
        kullanilacak_alanlar = operationSum(donecek_veri)
        for bar, total_per_gender in zip(bars, kullanilacak_alanlar):
            bar_height = bar.get_height()
            new_height = bar.get_height()
            bar.set_height(new_height)
            width = bar.get_width()
            x = bar.get_x()
            bar.set_width(width * width_scale)
            bar.set_x(x + width * (1 - width_scale) / 2)  # recenter
            if np.isnan(new_height):
                new_height = 0
            ax.text(x + width / hiza, bar_height - 1.6, f'{bar_height:.2f}%\n' if bar_height > 0.001 else '0%\n', ha='center', va='bottom', weight = 'bold', family = 'sans',fontsize = 60,rotation = 'horizontal')
    ax.set_title(yEkseni, loc = 'center', fontsize = 70, y= 1.2)
    ax.set_xlabel('')
    ax.set_ylabel('')
    ax.tick_params(axis='x', pad = 50, labelsize=textsize)
    ax.tick_params(axis='y', length=0, labelsize=60)
    ax.grid(axis='y', ls='-.',linewidth=3,  clip_on=True)
    siralama  = 0
    if len(veri[neyeGore].unique()) %2 == 0:
        siralama = 2
    else:
        siralama = 3
    sns.despine(fig, ax, top=True, right=True, left=True, bottom=True)
    ax.legend(ncol=siralama, title=neyeGore, title_fontsize=70 , fontsize = 70, bbox_to_anchor=(0.5, -0.1), loc='upper center', frameon=True, handlelength=1, handleheight=1) # changing legend
    ax.autoscale()
    ax.relim()
    ax.margins(y=0.05, x=0.09) 
    plt.tight_layout()
    plt.show()    

basitGorsel('Number',"Question",'Who', 1.8, 55)

And here is how the excel file looks like:

enter image description here

Thanks in advance.

Fatih Enes
  • 75
  • 5
  • 1
    Did you try `sns.barplot(...., dodge=False)`? Default seaborn wants to plot one bar for every hue value at every x-position and separate them a bit. With `dodge=False` all hues would be plotted without separating. – JohanC Apr 20 '21 at 08:26
  • Thank you JohanC. I cannot thank you enough. This answer solves my problem. – Fatih Enes Apr 20 '21 at 08:33

0 Answers0