首页 > 解决方案 > 主 x 轴在顶部,次 x 轴在底部

问题描述

我在这里没有找到答案,所以我将创建一个新答案。

我有两个 x 轴,我喜欢顶部的主 x 轴和底部的第二个 x 轴。怎么做?

这是我所做的,但没有奏效,它们都在底部。谢谢

ax.xaxis.tick_top()  # move primary xaxis to the top
ax1=ax.twiny()   # create 2nd x axis
ax1.xaxis.tick_bottom()   # move 2nd x axis to the bottom

标签: python

解决方案


正如所承诺的,一个工作示例:)

import numpy as np
import matplotlib.pyplot as plt

# data for primary x axis, note it is from 1 to 5
x1 = np.random.randint(1,6,10)
# data for secondary x axis, note it is from 6 to 10
x2 = np.random.randint(6,11,10)
# shared y
y1 = np.random.rand(10)
# primary on top and secondary on bottom
fig = plt.figure(figsize=(10,5))
ax = fig.add_subplot(1,1,1)
ax.scatter(x2,y1,c='red')
ax2 = ax.twiny()
ax2.scatter(x1,y1,c='blue')
plt.show()

在此处输入图像描述


推荐阅读