gpt4 book ai didi

function - 绘制上对数轴作为下线性轴的函数

转载 作者:行者123 更新时间:2023-12-04 07:48:01 24 4
gpt4 key购买 nike

我想绘制一个带有线性 x 轴和 y 轴的图,加上一个对数顶部 x 轴,显示刻度作为底部 x 轴的函数。不过,我不确定要传递给刻度的内容,或者是否单独定义函数来构建上对数轴刻度更方便(就像完成了 here )。我想要以 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()

最佳答案

副轴
更新:事实证明,使用 secondary_xaxis() 会简单得多。而不是 twiny() .您可以使用 functions参数指定底部和顶部轴之间的变换和反函数:

import matplotlib.pyplot as plt
import numpy as np

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

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

# secondary x-axis transformed with x*(a*b) and inverted with x/(a*b)
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.set_xscale('log')
axup.set_xlabel('Log axis')

plt.show()
secondary log axis with new params
原始示例:
# secondary x-axis transformed with x*a/b and inverted with x*b/a
ax1.set_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))
secondary log axis

打回来
您可以使用 Axes callbacks连接 ax1axup :

[The Axes callback] events you can connect to are xlim_changed and ylim_changed and the callback will be called with func(ax) where ax is the Axes instance.


这里 ax1.xlim_changed事件触发器 scale_axup()规模 axup.xlimscale(ax1.xlim) .请注意,我增加了 xlim最多 10 个以显示更多主要刻度:
from matplotlib.ticker import LogFormatterMathtext
import matplotlib.pyplot as plt
import numpy as np

fig, ax1 = plt.subplots(1, figsize=(15,9))

# axup scaler
scale = lambda x: x*1.e37/(2.*(3.809e8))

# set axup.xlim to scale(ax1.xlim)
def scale_axup(ax1):
# mirror xlim on both axes
left, right = scale(np.array(ax1.get_xlim()))
axup.set_xlim(left, right)

# set xticks to 0.1e28 intervals
xticks = np.arange(float(f'{left:.1e}'), float(f'{right:.1e}'), 0.1e28)
axup.set_xticks([float(f'{tick:.0e}') for tick in xticks])
axup.xaxis.set_major_formatter(LogFormatterMathtext())

# redraw to update xticks
axup.figure.canvas.draw()

# connect ax1 with axup (before ax1.set_xlim())
axup = ax1.twiny()
axup.set_xscale('log')
axup.set_xlabel(r'Log axis')
ax1.callbacks.connect(r'xlim_changed', scale_axup)

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

plt.show()

关于function - 绘制上对数轴作为下线性轴的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67113276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com