簡體   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