我试图画一个图形,两个x轴和两个y轴由函数连接,我使用下面的代码实现了这一点:
import numpy as np
import matplotlib.pyplot as plt
def q2r(q):
return(2*np.pi)/q
def r2q(r):
return(2*np.pi)/r
def w2t(w):
tau = 1/w
return(tau*(2/0.6583))
def t2w(t):
tau = t/(2/0.6583)
return(1/tau)
x = np.linspace(1,10)
y = x**2
fig, axs = plt.subplots(subplot_kw = {'xlabel':'$\omega / meV$', 'ylabel':'Q / $\AA^{-1}$'})
axs.plot(x,y)
secax_x = axs.secondary_xaxis('top', xlabel='t / ps', functions = (w2t, t2w))
secax_y = axs.secondary_yaxis('right', ylabel = 'r / $\AA$', functions = (q2r, r2q))
# sets ticks on primary axes
axs.set_xticks([2,5,8])
axs.set_yticks([20,50,80])
# does nothing
secax_x.set_xticks([3,1,0.5,0.1])
secax_y.set_yticks([1,0.2,0.01])
plt.show()
因为主轴和次轴之间的关系不是线性的,所以次轴上的刻度挤在一起(示例中的x2)或太少(示例中的y2)。我不希望刻度值相等,但希望手动控制次轴上的刻度位置。是否可以做到这一点?
1条答案
按热度按时间2sbarzqh1#
可以在次轴上使用对数刻度:
此快速修复解决了右y轴的问题。模拟然后解决次x轴的问题。