Answer a question

I'm having a problem with adding a logarithmic X-axis to my plot. I want to show results based on the sample size with methods A, B and C.

My dataframe:

            A         B         C
15   0.733333  0.613333  0.733333
30   0.716667  0.693333  0.766667
59   0.733684  0.678485  0.745763
118  0.796667  0.726087  0.779661
236  0.817862  0.788333  0.838983
470  0.832125  0.814468  0.836170

What I'm trying to make work:

sample_count = np.around(np.logspace(math.log10(15),math.log10(470),6))
sample_count = sample_count.astype(int)

sns.set_style('whitegrid')
g_results=sns.lineplot(data=results,dashes=0,markers=['o','o','o'])
g_results.set(xticks=sample_count)
g_results.set(xscale='log')

However the result is not what I exactly want, as the ticks are completely gone:

current output

Without the last xscale line it looks like this, which is the linear scale of course, but this time with the correct ticks:

current output without xscale

What I want to achieve is something like this:

desired output

I would appreciate your help with my problem.

Answers

First set the scale for the x-axis to logarithmic and then set xticks and labels as you want.

sns.set_style('whitegrid')
g_results=sns.lineplot(data=results,dashes=0,markers=['o','o','o'])
g_results.set(xscale='log')
g_results.set(xticks=sample_count)
g_results.set(xticklabels=sample_count)

This gives you this result:

resulting figure

Note that I'm using sample_count as defined with the logarithmic scale:

sample_count = np.around(np.logspace(math.log10(15),math.log10(470),6))
Logo

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐