matplotlib secondary_xaxis在子图中包含全局变量

pvcm50d1  于 6个月前  发布在  其他
关注(0)|答案(1)|浏览(54)

我试图做一个图,显示不同频谱的多普勒速度,但脚本似乎不喜欢我改变全局变量的事实。有没有办法解决这个问题?基本上,它只绘制全局变量的最新值的seonemic轴,见下图,顶部的一个甚至没有0。我猜它以某种方式追溯改变了以前的图。
之所以有一个glob,是因为我找不到一种方法来给予这个值,而不使secondary_xaxis函数崩溃。


的数据
最小工作示例:

def doppler(wavelengths):
    c = 299792.458 # speed of light in km/s
    lambda_0 = linecore # central wavelength in Angstrom
    doppler_shifts = c * ((wavelengths-lambda_0) / lambda_0)
    return doppler_shifts

def idoppler(doppler_shifts):
    c = 299792.458 # speed of light in km/s
    lambda_0 = linecore # central wavelength in Angstrom
    wavelengths = lambda_0 * (1 + doppler_shifts / c)-linecore
    return wavelengths

global linecore

plt.subplot(221)
plt.plot(np.linspace(-1,1,10)+6000, np.random.random([10]))
linecore = 6000
ax1 = plt.gca()  # Get the current axis (i.e., the one just created)
ax1a = ax1.secondary_xaxis('top', functions=(doppler, idoppler))
ax1a.set_xticks([-50,0,50])

plt.subplot(222)
plt.plot(np.linspace(-1,1,10)+6000, np.random.random([10]))
linecore = 6000
ax2 = plt.gca()  # Get the current axis (i.e., the one just created)
ax2a = ax2.secondary_xaxis('top', functions=(doppler, idoppler))
ax2a.set_xticks([-50,0,50])

plt.subplot(223)
plt.plot(np.linspace(-1,1,10)+8000, np.random.random([10]))
linecore = 8000
ax3 = plt.gca()  # Get the current axis (i.e., the one just created)
ax3a = ax3.secondary_xaxis('top', functions=(doppler, idoppler))
ax3a.set_xticks([-50,0,50])

plt.subplot(224)
plt.plot(np.linspace(-1,1,10)+8000, np.random.random([10]))
linecore = 8000
ax4 = plt.gca()  # Get the current axis (i.e., the one just created)
ax4a = ax4.secondary_xaxis('top', functions=(doppler, idoppler))
ax4a.set_xticks([-50,0,50])

plt.tight_layout()
plt.show()

字符串

jvlzgdj9

jvlzgdj91#

你应该用咖喱。


的数据

import matplotlib.pyplot as plt
import numpy as np

C = 299792.458 # speed of light in km/s

def doppler_shifts(wavelengths, λ0):
    return C*wavelengths/λ0 - C

def wavelenghts(doppler_shifts, λ0):
    return λ0*doppler_shifts/C + λ0

y = iter([np.random.random(10) for _ in range(4)])

fig, ax_2D = plt.subplots(2,2, layout='constrained')

for ax_row, λ0 in zip(ax_2D, (6000, 8000)):
    wl = λ0+np.linspace(-1, 1, 10)
    # here comes the currying
    # note we must trick lambda, otherwise late binding of λ0 is a problem
    f0 = lambda wl, λ0=λ0: doppler_shifts(wl, λ0)
    f1 = lambda ds, λ0=λ0: wavelenghts(ds, λ0)     
    for ax1 in ax_row:
        ax1.plot(wl, next(y))
        ax2 = ax1.secondary_xaxis('top', functions=(f0, f1))
        ax1.set_xlabel('Wave Lenghts')
        ax2.set_xlabel('Doppler Shifts')
        
plt.show()

字符串

相关问题