Seaborn lineplot Y-axis values to 1 decimal place – code not working but not sure why

Question:

I have the following code.

I am trying to plot a lineplot using seaborn.

However, I want all the Y-values to be to 1 decimal place.

When I try to set all these values to 1 decimal place, this does not seem to work.

I would be so grateful for a helping hand!

listedvariables = ['gender']
newestdf[['distance']] = newestdf[['distance']].round(1)
for i in range(0,len(listedvariables)): 
    plt.figure(figsize=(50,50))
    ax = sns.lineplot(data=newestdf,x=listedvariables[i],y="distance",errorbar ='se',err_style='bars',linewidth=2)
    ax.set_xlabel(listedvariables[i],labelpad = 40,fontsize=70,weight='bold')
    ax.set_ylabel("Wayfinding Distance",labelpad = 40,fontsize=70,weight='bold')
    ax.set_xticklabels(ax.get_xticks(),rotation = 30, ha="right",fontsize=60,weight='bold')
    ax.set_yticklabels(ax.get_yticks(),ha="right",fontsize=60,weight='bold')
    title = (listedvariables[i] + ' ' + 'plot')
    ax.set_title(title,fontsize=70,pad=40,weight='bold') 
    dir_name = "/Users/macbook/Desktop/PhD Work/"
    plt.rcParams["savefig.directory"] = os.chdir(os.path.dirname(dir_name))
    plt.savefig(listedvariables[i]+' '+'scatterplot')
    plt.show()

Plot:

enter image description here

As an example:

newestdf[['distance']].round(1)

0     -0.3
1      0.1
2     -0.2
3     -0.6
5      1.1
      ... 
911   -0.2
912    0.2
913   -0.3
914   -0.4
915   -0.3
Asked By: Caledonian26

||

Answers:

Add these two lines (before title variable setup in your code):

ylabels = ['{:,.2f}'.format(x) for x in ax.get_yticks()]
ax.set_yticklabels(ylabels)

Hope this helps!

Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.