gpt4 book ai didi

python - 多图中刻度线的长度相同

转载 作者:行者123 更新时间:2023-12-01 06:45:39 28 4
gpt4 key购买 nike

如何设置所有子图中刻度/刻度的长度相同?我想根据第四个子图设置所有 xticks 的长度。我的意思是,所有名为 y 的轴在刻度 0 和 2 之间具有相同的间距,所有名为 x 的轴在 -1 和 0 之间具有相同的间距。也许将绘图设置为正方形就足够了。请问怎么样?

import numpy as np
import matplotlib.pyplot as plt
from numpy import array
import matplotlib as mpl

x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

# Plot figure with size
fig, ax = plt.subplots(sharex=True)
plt.figure(figsize=(12, 9))

# Subplots
fig1 = plt.subplot(231)
plt.plot(x, y**2)
fig1.set_xlim(0e-13,2e-13)
fig1.set_ylim(-1.15e-14,0.01e-14)

fig2=plt.subplot(232)
plt.plot(x, y**2)
fig2.set_xlim(0e-13,2e-13)
fig2.set_ylim(-7.3e-15,7.3e-15)

fig3=plt.subplot(233)
plt.plot(x, y**2)
fig3.set_ylim(0e-13,1.2e-13)
fig3.set_xlim(0e-13,2e-13)

# Subplots with arrows
fig4=plt.subplot(234)
plt.plot(x, y**2)
fig4.set_xlim(-1.15e-14,0.01e-14)
fig4.set_ylim(-7.3e-15,7.3e-15)

fig5=plt.subplot(235)
plt.plot(x, y**2)
fig5.set_xlim(-7.3e-15,7.3e-15)
fig5.set_ylim(0e-13,1.2e-13)
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)

fig6=plt.subplot(236)
plt.plot(x, y**2)
fig6.set_xlim(-1.5e-14,0e-14)
fig6.set_ylim(0e-13,1.2e-13)

plt.show()

enter image description here

最佳答案

this excellent answer 最好地概述了实现这一目标的方法。通过 @ImportanceOfBeingErnest 。基本上,您可以手动计算缩放比例,以遵循每个现有轴的 yx 限制之间的比率,例如

fig1.set_aspect(np.diff(fig1.get_xlim())/np.diff(fig1.get_ylim()))

但请注意,这必须在调用 set_ylim()set_xlim() 之后完成,因为它必须使用最终限制以便正确计算所需的比例。 set_xticks()set_yticks() 可以在之前或之后安全地调用,效果相同。

将其应用于六个中的每一个都会产生

enter image description here

关于python - 多图中刻度线的长度相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59233199/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com