Search code examples
pythonmatplotlibseabornxticks

Setting xticks results in only a single tick on the x-axis


I'm using the code below to produce a chart in seaborn. I have included the values for score_train, score_test and alphas so that you could replicate if you try to run my code.

alphas = [0.00000000e+00, 3.12213617e-05, 3.41931065e-05, 9.09382510e-05,
       1.42111604e-04, 1.50323881e-04, 2.89300778e-04, 3.20398837e-04,
       3.26079390e-04, 3.67602501e-04, 4.24184679e-04, 4.34481263e-04,
       4.90132981e-04, 5.62078948e-04, 5.85072664e-04, 6.78360178e-04,
       7.93980204e-04, 8.07864745e-04, 9.61382275e-04, 1.29066450e-03,
       1.40562637e-03, 1.46742025e-03, 1.66062768e-03, 1.72203321e-03]

score_train = [0.0104715802718085,
 0.010458280337911519,
 0.010443714475584676,
 0.010443714475584676,
 0.010344437970874143,
 0.010280401760789704,
 0.010280401760789704,
 0.010157163022360027,
 0.009881770880034169,
 0.00988177088003428,
 0.0097251765261126,
 0.009544478827850478,
 0.008756729180697365,
 0.008038412062383204,
 0.008038412062383316,
 0.007789177969584338,
 0.007211231010840424,
 0.006528863850344235,
 0.006528863850344235,
 0.0055695183357952205,
 0.0055695183357952205,
 0.0043456341750442995,
 0.0043456341750444105,
 0.0014375284078337325]

score_test = [-0.013180580298218336,
 -0.013160472761265751,
 -0.013151821204646863,
 -0.013151821204646863,
 -0.013165861122794942,
 -0.013197005960391195,
 -0.013197005960391195,
 -0.013046889005794426,
 -0.013122528043505444,
 -0.013122528043505444,
 -0.013673242248775308,
 -0.013585443618303605,
 -0.01417799133998554,
 -0.014195521870022931,
 -0.01419552187002271,
 -0.009323258408655777,
 -0.0023269067131193033,
 -0.00362963279693429,
 -0.003629632796934512,
 -0.0045420007912404525,
 -0.0045420007912404525,
 -0.0018079981576566428,
 -0.0018079981576566428,
 -0.00034014856624420275]

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

sns.set()
plt.figure(figsize=(14,7))
sns.lineplot(y=score_train,x=alphas,label="Training Score")
sns.lineplot(y=score_test,x=alphas,label="Testing Score")
plt.xticks(ticks=np.arange(0.000000,0.0001,0.001000))
plt.show()

Here is a picture of what I get:

Picture of chart with broken x-axis

As you can see I cannot see the values on the x-axis. What am I doing wrong here?


Solution

  • np.arange(0.000000,0.0001,0.001000) is array([0.]), because your step (0.001) is greater than the difference between start (0.0) and stop (0.0001).

    You might want to use np.arange(0., 0.001, 0.0001) instead.