Python中plt.xticks的整数值

我想绘制一个图,然后将plt.xticks设置为np.linspace(1、3、10)。

我认为x轴上的值为1,1.2,1.4,1.6,...,3。

但结果是:1.000、1.222、1.444、1.667,...,3.000。

我怎样才能解决这个问题?

import numpy as np
import matplotlib.pyplot as plt

n_c = 1
n_s = np.linspace(1, 3, 100)

iso_trapping_efficiency = (np.sqrt(1 - (n_c ** 2 / n_s ** 2))) *100

plt.figure()
plt.plot(n_s, iso_trapping_efficiency, 'r', label='Isotropic Dipole')

plt.xlabel('Refractive Index, n_s', fontsize=15)
plt.ylabel('Trapping Efficiency [%]', fontsize=15)

# change the xticks
plt.xticks(np.linspace(1, 3, 10))

plt.legend(loc='lower right')

plt.show()

enter image description here

评论
  • ueos
    ueos 回复

    np.linspace(1, 3, 10) creates 10 samples (interval boundaries) in the interval (1,3). Note that it includes beginning and end of the interval in the samples. Therefore, 10 samples of interval boundaries will create 10-1=9 intervals (think of it as a single interval has two boundaries). If you need to split it into 10 intervals, call it for 10+1 samples:

     np.linspace(1, 3, 11)
    
  • nrerum
    nrerum 回复

    The third parameter of np.linspace() does not include the value. Try plt.xticks(np.linspace(1, 3, 11))