繁体   English   中英

不同范围的 x 轴

[英]different ranges of x-axes

请问如何切换x轴上的刻度而不改变plot? 我尝试了命令的行。

import matplotlib.pyplot as plt

x1 = [1, 2, 3]
x2 = [1, 2, 3, 4, 5, 6]
y2 = [1.2, 2, 2.5, 3.0, 3.1, 5.3]

fig, ax = plt.subplots(figsize=(10, 6))

plt.xlim(-2,2+max(len(x1), len(x2)))
plt.scatter(x1, x1, c= 'red')
plt.scatter(x2, y2, c = 'green')

ax.tick_params(axis='x')

ax2 = ax.twinx()
ax3 = ax.twiny()
ax2.set_ylim(ax.get_ylim())
ax3.set_xlim(-2,2+len(x1))
ax3.tick_params(axis='x')
ax.tick_params(axis='x', colors='red')
ax3.tick_params(axis='x', colors='green')
#ax.set_xlim(-2,2+len(x1))
#ax3.set_xlim(-2,2+len(x2))

plt.show()

我想要下轴数据 x1 的 -2,2+len(x1) 范围和上轴 x2 的 -2,2+len(x2) 范围,并将其分布在 -2,2+ 的范围内最大(长度(x2),长度(x1))。

在此处输入图像描述

期望的结果是这样的(请切换刻度和标签,不要考虑线轴上的错误 position):

在此处输入图像描述

import matplotlib.pyplot as plt

x1 = [1, 2, 3]
x2 = [1, 2, 3, 4, 5, 6]
y2 = [1.2, 2, 2.5, 3.0, 3.1, 5.3]

fig, ax = plt.subplots(figsize=(10, 6))


ax.tick_params(axis='x')
ax.scatter(x1, x1, c= 'red')
ax.set_xlim(-2,2+len(x1))


ax3 = ax.twiny()
ax3.scatter(x2, y2, c = 'green')
ax3.tick_params(axis='x')
ax.tick_params(axis='x', colors='red')
ax3.tick_params(axis='x', colors='green')
ax3.set_xlim(-2,2+len(x2))

plt.show()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM