在Matplotlib中的插图中使用twiny()

时间:2018-12-07 09:41:38

标签: python-3.x matplotlib

我正在尝试将第二个x轴添加到我从InsetPositionmpl_toolkits.axes_grid1.inset_locator创建的插图中(例如https://scipython.com/blog/inset-plots-in-matplotlib/之后),但是第二个x轴没有似乎没有出现,我不知道为什么。

这是我正在使用的代码:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.gca()

from mpl_toolkits.axes_grid1.inset_locator import InsetPosition
zoom_ax = fig.add_axes([0,0,1,1])
zoom_ax.set_axes_locator(InsetPosition(ax, [0.6, 0.6, 0.3, 0.3]))

def expansion(z):
    return 1.0 / (1.0 + z)

def redshift(a):
    return 1.0 / a - 1.0

def tick_function(a):
    return ["%.1f" % z for z in redshift(a)]

z_ticks = np.array([0.0, 0.5, 1.0, 2.0, 5.0, 100.0])
a_ticks = expansion(z_ticks)

twin_ax = zoom_ax.twiny()
twin_ax.set_xticks(a_ticks)
twin_ax.set_xticklabels(tick_function(a_ticks))
twin_ax.set_xlim(zoom_ax.get_xlim())

xmin, xmax = 0.0, 1.0
x = np.linspace(xmin, xmax)
zoom_ax.plot(x, np.sin(x))
zoom_ax.set_xlim(xmin, xmax)

plt.show()

这将产生以下图-没有任何twiny()轴:

result of the above code

2 个答案:

答案 0 :(得分:2)

显然,twiny()axes_locator使用的zoom_ax有问题(不知道这是否是错误)。如果您对set_axes_locator()重复使用twin_ax命令,则结果图看起来就像我期望的那样(我省略了轴ticks命令以使示例图更容易理解):

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.gca()

from mpl_toolkits.axes_grid1.inset_locator import InsetPosition
zoom_ax = fig.add_axes([0,0,1,1])
zoom_ax.set_axes_locator(InsetPosition(ax, [0.6, 0.6, 0.3, 0.3]))

def expansion(z):
    return 1.0 / (1.0 + z)

def redshift(a):
    return 1.0 / a - 1.0

def tick_function(a):
    return ["%.1f" % z for z in redshift(a)]

z_ticks = np.array([0.0, 0.5, 1.0, 2.0, 5.0, 100.0])
a_ticks = expansion(z_ticks)

twin_ax = zoom_ax.twiny()
##twin_ax.set_xticks(a_ticks)
##twin_ax.set_xticklabels(tick_function(a_ticks))
twin_ax.set_xlim(zoom_ax.get_xlim())

xmin, xmax = 0.0, 1.0
x = np.linspace(xmin, xmax)
zoom_ax.plot(x, np.sin(x))
zoom_ax.set_xlim(xmin, xmax)

##the extra lines
twin_ax.set_axes_locator(InsetPosition(ax, [0.6, 0.6, 0.3, 0.3]))
x2 = np.linspace(xmin, 2*xmax)
twin_ax.plot(x2,np.cos(x2),'r')
twin_ax.set_xlim(xmin, 2*xmax)

plt.show()

这将产生以下情节:

result of the above code

答案 1 :(得分:1)

也许您想使用通常的mpl_toolkits.axes_grid1.inset_locator.inset_axes,即使孪生也能正常工作。

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.gca()

from mpl_toolkits.axes_grid1.inset_locator import inset_axes
zoom_ax = inset_axes(ax, "100%", "100%", bbox_to_anchor=[0.6, 0.6, 0.3, 0.3], 
                     bbox_transform=ax.transAxes)

def expansion(z):
    return 1.0 / (1.0 + z)

def redshift(a):
    return 1.0 / a - 1.0

def tick_function(a):
    return ["%.1f" % z for z in redshift(a)]

z_ticks = np.array([0.0, 0.5, 1.0, 2.0, 5.0, 100.0])
a_ticks = expansion(z_ticks)

twin_ax = zoom_ax.twiny()
twin_ax.set_xticks(a_ticks)
twin_ax.set_xticklabels(tick_function(a_ticks))
twin_ax.set_xlim(zoom_ax.get_xlim())

xmin, xmax = 0.0, 1.0
x = np.linspace(xmin, xmax)
zoom_ax.plot(x, np.sin(x))
zoom_ax.set_xlim(xmin, xmax)

plt.show()

从matplotlib 3.0开始,您可以使用Axes.inset_axes进一步简化此操作:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.gca()

zoom_ax = ax.inset_axes([0.6, 0.6, 0.3, 0.3])

def expansion(z):
    return 1.0 / (1.0 + z)

def redshift(a):
    return 1.0 / a - 1.0

def tick_function(a):
    return ["%.1f" % z for z in redshift(a)]

z_ticks = np.array([0.0, 0.5, 1.0, 2.0, 5.0, 100.0])
a_ticks = expansion(z_ticks)

twin_ax = zoom_ax.twiny()
twin_ax.set_xticks(a_ticks)
twin_ax.set_xticklabels(tick_function(a_ticks))
twin_ax.set_xlim(zoom_ax.get_xlim())

xmin, xmax = 0.0, 1.0
x = np.linspace(xmin, xmax)
zoom_ax.plot(x, np.sin(x))
zoom_ax.set_xlim(xmin, xmax)

plt.show()

结果在视觉上是相同的:

enter image description here