Function 将上对数轴作为下线性轴的函数进行绘图

Function 将上对数轴作为下线性轴的函数进行绘图,function,matplotlib,axes,logarithm,Function,Matplotlib,Axes,Logarithm,我想用线性x轴和y轴绘制一个图,再加上一个对数顶部x轴,显示刻度作为底部x轴的函数。我不确定传递给刻度的是什么,或者单独定义构建上对数轴刻度的函数是否更方便(类似于这样做)。我希望上对数轴上的刻度以0.1的步长显示。 这是一个MWE: from matplotlib.ticker import ScalarFormatter, FormatStrFormatter import matplotlib.pyplot as plt import matplotlib.ticker as ticke

我想用线性x轴和y轴绘制一个图,再加上一个对数顶部x轴,显示刻度作为底部x轴的函数。我不确定传递给刻度的是什么,或者单独定义构建上对数轴刻度的函数是否更方便(类似于这样做)。我希望上对数轴上的刻度以
0.1
的步长显示。 这是一个MWE:

from matplotlib.ticker import ScalarFormatter, FormatStrFormatter

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np

fig, ax1 = plt.subplots(1, figsize=(10,6))

ax1.set_ylabel(r'y axis')
ax1.set_xlabel(r'Linear axis')
ax1.set_ylim(0.1,1.)
ax1.set_xlim(0.1,1.5)

#Upper lox-axis
new_tick_locations = 
[np.log(i*1.e37/(2.*(3.809e8))) for i in np.arange(0.1, 10., 0.1)] #I should pass something else instead of arange
                                                  #I'd like the upper axis ticks in steps of 0.1 anyway

axup=ax1.twiny()
axup.set_xticks(new_tick_locations)

axup.set_xlabel(r'Log axis')
plt.show()
通过遵循您共享的代码,我根据您的需要修改了代码

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np

from matplotlib.ticker import StrMethodFormatter

fig, ax1 = plt.subplots(1, figsize=(10,6))

ax1.set_ylabel(r'y axis')
ax1.set_xlabel(r'Linear axis')

ax1.set_xlim(0.1,1.5)

#Upper lox-axis
def tick_function(x):
    v = np.log(x*1.e37/(2.*(3.809e8)))
    return ["%.1f" % z for z in v]


axup_locations = np.arange(0.1, 10., 0.1)


axup=ax1.twiny()

axup.set_xscale('log')
axup.set_xlim(0.1,100)
axup.set_yscale('linear')
axup.xaxis.set_major_formatter(StrMethodFormatter('{x:.0f}'))


axup.set_xlabel(r'Log axis')
plt.show()

次轴 更新:事实证明,这比使用
twiny()要简单得多。
。您可以使用
函数
参数指定底轴和顶轴之间的变换和反函数:

导入matplotlib.pyplot作为plt
将numpy作为np导入
图,ax1=plt.子批次(1,figsize=(10,6))
ax1.set_ylabel(“y轴”)
ax1.set_xlabel('线性轴')
ax1.set_ylim(0.1,1.)
ax1.set_xlim(0.1e-9,1.5e-9)
#用x*(a*b)变换并用x/(a*b)反转的次x轴
a、 b=4.*np.pi,np.float64((2.*3.086e22)**2。)
axup=ax1.secondary_xaxis('top',functions=(lambda x:x*(a*b),lambda x:x/(a*b)))
axup.setxscale('log'))
axup.set_xlabel('对数轴')
plt.show()

原始示例:

#用x*a/b变换的次x轴和用x*b/a反转的次x轴
ax1.设置\u xlim(0.1,10.)
a、 b=1.e37,2.*(3.809e8)
axup=ax1.secondary_xaxis('top',functions=(lambda x:x*a/b,lambda x:x*b/a))


回拨 您可以使用连接
ax1
axup

[您可以连接到的
Axes
callback]事件是
xlim\u changed
ylim\u changed
,将使用
func(ax)
调用回调,其中
ax
Axes
实例

此处,
ax1.xlim\u已更改
事件将触发
scale\u axup()
以缩放
axup.xlim
scale(ax1.xlim)
。请注意,我将
xlim
增加到10,以演示更多的主要刻度:

从matplotlib.ticker导入LogFormatterMathtext
将matplotlib.pyplot作为plt导入
将numpy作为np导入
图,ax1=plt.子批次(1,figsize=(15,9))
#axup定标器
刻度=λx:x*1.e37/(2.*(3.809e8))
#将axup.xlim设置为缩放(ax1.xlim)
def比例放大(ax1):
#在两个轴上镜像xlim
左、右=缩放(np.array(ax1.get_xlim()))
axup.set_xlim(左、右)
#将xticks设置为0.1e28间隔
xticks=np.arange(float(f'{left:.1e}'),float(f'{right:.1e}'),0.1e28)
set_xticks([float(f'{tick:.0e}')用于xticks中的tick])
axup.xaxis.set_major_格式化程序(LogFormatterMathtext())
#重新绘制以更新xticks
axup.figure.canvas.draw()
#将ax1与axup连接(在ax1.set_xlim()之前)
axup=ax1.twiny()
axup.setxscale('log'))
axup.set_xlabel(r‘对数轴’)
ax1.回调.连接(r'xlim\u changed',scale\u axup)
ax1.设置标签(y轴)
ax1.设置标签(r‘线性轴’)
ax1.set_ylim(0.1,1.)
ax1.设置\u xlim(0.1,10.)
plt.show()

谢谢。但是,我希望这些刻度像对数轴刻度一样分开(例如),我明白你的意思。我刚刚更新了我的答案,这就是你想要的吗?这更接近,但它改变了yaxis上的比例,在我的例子中,它绘制了一个函数,我不确定它来自哪里。查看图形中的y轴,并将其与
ax1.set_ylim(0.1,1.)
进行比较。此外,如果可能的话,顶部的刻度不应该是scientific notation.Updated。不过,我对勾号功能有点困惑。通过使用它,您到底想在axup上绘制什么?删除科学符号。谢谢。但这根本不考虑函数
tick\u函数
axup
显示
axup=f(ax1)
上的值的函数。谢谢。但是,这样axup刻度就不会像日志刻度那样分开,例如,@Py ser不确定我是否理解。使用您的换算公式,
axup
刻度在60的范围内。根据您的其他评论,我认为您希望
axup
在线性轴和转换公式之间有对应关系,例如0.1->62.44、0.8->64.52、1.4->65.08等。让我们来看看。