【问题标题】:How to add second x-axis at the bottom of the first one in matplotlib.?如何在 matplotlib 的第一个 x 轴的底部添加第二个 x 轴。?
【发布时间】:2015-08-04 08:02:41
【问题描述】:

我指的是已经问过的问题here
在这个例子中,用户解决了第二个轴的问题,方法是将它添加到图表的上部,它与标题重合。

问题: 是否可以在第一个 x 轴的底部添加第二个 x 轴?

代码:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()

X = np.linspace(0,1,1000)
Y = np.cos(X*20)

ax1.plot(X,Y)
ax1.set_xlabel(r"Original x-axis: $X$")

new_tick_locations = np.array([.2, .5, .9])

def tick_function(X):
    V = 1/(1+X)
    return ["%.3f" % z for z in V]

ax2.set_xticks(new_tick_locations)
ax2.set_xticklabels(tick_function(new_tick_locations))
ax2.set_xlabel(r"Modified x-axis: $1/(1+X)$")
plt.show()

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    作为@DizietAsahi 答案的替代方案,您可以使用spines,其方式与matplotlib 发布的here 示例类似。

    import numpy as np
    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax2 = ax1.twiny()
    
    # Add some extra space for the second axis at the bottom
    fig.subplots_adjust(bottom=0.2)
    
    X = np.linspace(0,1,1000)
    Y = np.cos(X*20)
    
    ax1.plot(X,Y)
    ax1.set_xlabel(r"Original x-axis: $X$")
    
    new_tick_locations = np.array([.2, .5, .9])
    
    def tick_function(X):
        V = 1/(1+X)
        return ["%.3f" % z for z in V]
    
    # Move twinned axis ticks and label from top to bottom
    ax2.xaxis.set_ticks_position("bottom")
    ax2.xaxis.set_label_position("bottom")
    
    # Offset the twin axis below the host
    ax2.spines["bottom"].set_position(("axes", -0.15))
    
    # Turn on the frame for the twin axis, but then hide all 
    # but the bottom spine
    ax2.set_frame_on(True)
    ax2.patch.set_visible(False)
    
    # as @ali14 pointed out, for python3, use this
    # for sp in ax2.spines.values():
    # and for python2, use this
    for sp in ax2.spines.itervalues():
        sp.set_visible(False)
    ax2.spines["bottom"].set_visible(True)
    
    ax2.set_xticks(new_tick_locations)
    ax2.set_xticklabels(tick_function(new_tick_locations))
    ax2.set_xlabel(r"Modified x-axis: $1/(1+X)$")
    plt.show()
    

    【讨论】:

    • 工作正常。在 Python 3.x 中使用 for sp in ax2.spines.values():
    【解决方案2】:

    我认为您必须创建第二个高度为 0 的 Axes(并隐藏 yaxis)才能拥有第二个 xaxis,您可以将其放置在您喜欢的任何位置。

    例如:

    import numpy as np
    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    ax1 = fig.add_axes((0.1,0.3,0.8,0.6)) # create an Axes with some room below
    
    X = np.linspace(0,1,1000)
    Y = np.cos(X*20)
    
    ax1.plot(X,Y)
    ax1.set_xlabel(r"Original x-axis: $X$")
    
    
    # create second Axes. Note the 0.0 height
    ax2 = fig.add_axes((0.1,0.1,0.8,0.0))
    ax2.yaxis.set_visible(False) # hide the yaxis
    
    new_tick_locations = np.array([.2, .5, .9])
    
    def tick_function(X):
        V = 1/(1+X)
        return ["%.3f" % z for z in V]
    
    ax2.set_xticks(new_tick_locations)
    ax2.set_xticklabels(tick_function(new_tick_locations))
    ax2.set_xlabel(r"Modified x-axis: $1/(1+X)$")
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2012-05-17
      • 2015-12-16
      • 2014-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多