【问题标题】:Same length of ticks in multiplot多图中相同长度的刻度
【发布时间】:2019-12-08 07:05:22
【问题描述】:

如何设置在所有子图中具有相同的刻度/长度?我想根据第四个子图设置所有 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()

【问题讨论】:

    标签: python matplotlib subplot


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 这不是重复这个问题吗?
    • @AlexanderCécile 老实说,由于问题和标题的措辞,我不确定我知道要问什么,因此我没有信心将其与级别的链接问题进行比较肯定会举旗 - 尤其是考虑到有 6k 人正在等待接近投票队列。
    • @AlexanderCécile 几天前是 8k,您需要 3k 代表来查看这些标志。与我的 12 岁相比,33 岁还不错......
    • @WilliamMiller 等等,你怎么只有 12 个?这个网站只是令人沮丧,伙计......
    • @WilliamMiller 啊,对了,您会根据净有用标志的数量获得更多标志,而不仅仅是声望。好吧,你最好开始处理那个接近的投票队列,我正在加班;)
    猜你喜欢
    • 2017-01-15
    • 2014-04-12
    • 2023-03-19
    • 2021-11-05
    • 1970-01-01
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    相关资源
    最近更新 更多