我正在从@Dhara答案中的注释中获取线索,这听起来像是您要按
new_tick_locations功能设置从旧x轴到新x轴的列表。在
tick_function下面发生在点的numpy的阵列,它们映射到一个新的值,并且将它们格式化:
import numpy as npimport matplotlib.pyplot as pltfig = plt.figure()ax1 = fig.add_subplot(111)ax2 = ax1.twiny()X = np.linspace(0,1,1000)Y = np.cos(X*20)ax1.plot(X,Y)ax1.set_xlabel(r"Original x-axis: $X$")new_tick_locations = np.array([.2, .5, .9])def tick_function(X): V = 1/(1+X) return ["%.3f" % z for z in V]ax2.set_xlim(ax1.get_xlim())ax2.set_xticks(new_tick_locations)ax2.set_xticklabels(tick_function(new_tick_locations))ax2.set_xlabel(r"Modified x-axis: $1/(1+X)$")plt.show()



