【问题标题】:different ranges of x-axes不同范围的 x 轴
【发布时间】:2022-08-09 20:51:58
【问题描述】:

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

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))。

    标签: python-3.x matplotlib axes


    【解决方案1】:

    检查代码的正确位置是:

    https://codereview.stackexchange.com/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-06
      相关资源
      最近更新 更多