python - 主 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
解决方案
正如所承诺的,一个工作示例:)
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()
推荐阅读
- spring-boot - 在请求参数“_csrf”或标头“X-CSRF-TOKEN”上发现无效的 CSRF 令牌“null”.. Spring-boot
- excel - 根据另一个单元格值将单元格值分组到范围
- java - Auth0 升级到 Node8
- mongodb-atlas - 将 Google Cloud Run 连接到 MongoDB Atlas
- python - zip 文件正在压缩到默认项目目录文件夹而不是文件目录
- node.js - 环回电子邮件连接器不发送电子邮件
- google-apps-script - 如何自动复制超链接,使其链接到相同的重复页面而不是 Google 表格中的原始页面?
- hl7-fhir - Sending data (non-standard) headers in FHIR request
- typescript - How to restrict to generic types to be the same subtype of another generic type?
- sql - 如何在 SQL 中从 XML 中获取值