matplotlib - sharey ='row' 和 sharey ='True' 的区别

标签 matplotlib subplot

我正在考虑一个包含 3 行和 4 列的图,其中:

有 3 个因变量要绘制:Y1Y2Y3,与普通的X 独立变量,对于 4 个研究案例:

enter image description here

在这种情况下有:

1) 从 case icase i+1

共享 y

2) 在 case i

中共享 x

因此,原则上,人们会认为以下代码将产生所需的图(结果如上图所示):

fig, axes = plt.subplots(ncols=4, nrows=3,\
                         sharex=True, sharey=True,\
                         subplot_kw=dict(adjustable='box-forced'))

adjustable='box-forced' 只是为了确保子图是平方的,如 here 所解释的那样.

当我尝试为情况 1 绘制 Y1X 时:

import numpy as np
import matplotlib.pyplot as plt
import sys

fig, axes = plt.subplots(ncols=4, nrows=3,\
                         sharex=True, sharey=True,\
                         subplot_kw=dict(adjustable='box-forced'))

pad = 5
axes[0][0].annotate('Case 1', xy=(0.5, 1), xytext=(0, pad),
                xycoords='axes fraction', textcoords='offset points',
                 size='large', ha='center', va='baseline')

axes[0][1].annotate('Case 2', xy=(0.5, 1), xytext=(0, pad),
                xycoords='axes fraction', textcoords='offset points',
                 size='large', ha='center', va='baseline')

axes[0][2].annotate('Case 3', xy=(0.5, 1), xytext=(0, pad),
               xycoords='axes fraction', textcoords='offset points',
                size='large', ha='center', va='baseline')

axes[0][3].annotate('Case 4', xy=(0.5, 1), xytext=(0, pad),
                xycoords='axes fraction', textcoords='offset points',
                 size='large', ha='center', va='baseline')

#
axes[0][0].set_ylabel('Y1', fontsize=10)
axes[1][0].set_ylabel('Y2', fontsize=10)
axes[2][0].set_ylabel('Y3', fontsize=10)

E_C_I =  np.array([-941.23658347, -941.23685494, -941.23467666])
V_C_I =  np.array([ 61.66341, 62.342903,  67.9311515])
E_14 =  np.array([-941.22938469, -941.23583586, -941.23605613])
V_14 =  np.array([ 54.65693125,  58.47115725, 60.8626545 ])
P_C_I =  np.array([ 2.20068119,  1.33328211,  -4.28370285])
P_14 =  np.array([ 8.16605135,  7.54737315, 0.3909309 ])


axes[0][0].scatter(V_C_I, E_C_I, marker='^', color='red', label='Calcite I')#, s=100)
axes[0][0].scatter(V_14, E_14, marker='o', color='green', label='Calcite I')#, s=100)

axes[0][0].set_ylim(bottom=-941.238, top=-941.229)

plt.tight_layout()
axes[0][0].ticklabel_format(useOffset=False)
plt.show()
sys.exit()

一切似乎都很好:

enter image description here

我已将绘图强制设置为 axes[0][0].set_ylim(bottom=-941.238, top=-941.229)

当我尝试为 Case 1 绘制 Y2X 时,以下代码应该有效:我基本上和以前一样,但添加了 axes[1][0] 绘图指令:

import numpy as np
import matplotlib.pyplot as plt
import sys

fig, axes = plt.subplots(ncols=4, nrows=3,\
                         sharex=True, sharey=True,\
                         subplot_kw=dict(adjustable='box-forced'))

pad = 5
axes[0][0].annotate('Case 1', xy=(0.5, 1), xytext=(0, pad),
                xycoords='axes fraction', textcoords='offset points',
                 size='large', ha='center', va='baseline')

axes[0][1].annotate('Case 2', xy=(0.5, 1), xytext=(0, pad),
                xycoords='axes fraction', textcoords='offset points',
                 size='large', ha='center', va='baseline')

axes[0][2].annotate('Case 3', xy=(0.5, 1), xytext=(0, pad),
               xycoords='axes fraction', textcoords='offset points',
                size='large', ha='center', va='baseline')

axes[0][3].annotate('Case 4', xy=(0.5, 1), xytext=(0, pad),
                xycoords='axes fraction', textcoords='offset points',
                 size='large', ha='center', va='baseline')

#
axes[0][0].set_ylabel('Y1', fontsize=10)
axes[1][0].set_ylabel('Y2', fontsize=10)
axes[2][0].set_ylabel('Y3', fontsize=10)

E_C_I =  np.array([-941.23658347, -941.23685494, -941.23467666])
V_C_I =  np.array([ 61.66341, 62.342903,  67.9311515])
E_14 =  np.array([-941.22938469, -941.23583586, -941.23605613])
V_14 =  np.array([ 54.65693125,  58.47115725, 60.8626545 ])
P_C_I =  np.array([ 2.20068119,  1.33328211,  -4.28370285])
P_14 =  np.array([ 8.16605135,  7.54737315, 0.3909309 ])


axes[0][0].scatter(V_C_I, E_C_I, marker='^', color='red', label='Calcite I')#, s=100)
axes[0][0].scatter(V_14, E_14, marker='o', color='green', label='Calcite I')#, s=100)

axes[0][0].set_ylim(bottom=-941.238, top=-941.229)

axes[1][0].scatter(V_C_I, P_C_I, marker='^', color='red', label='Calcite I')#, s=100)
axes[1][0].scatter(V_14, P_14, marker='o', color='green', label='Calcite I')#, s=100)

axes[1][0].set_ylim(bottom=-4.4, top=8.4)

plt.tight_layout()
axes[0][0].ticklabel_format(useOffset=False)
plt.show()
sys.exit()

结果是 axes[0][0] 图改变了比例,因此没有显示数据:

enter image description here

我已经强制 axes[0][0]axes[0][1] 显示确实有数据的区域:

axes[0][0].set_ylim(bottom=-941.238, top=-941.229)
axes[1][0].set_ylim(bottom=-4.4, top=8.4)

但是,axes[0][0] 图中未显示任何数据。为什么会这样?

更新:sharey='row'sharey=True 之间的区别已在出色的@DavidG 回答中阐明。但是,我测试了 sharex='col'sharex=True 之间的区别,我注意到:

fig, axes = plt.subplots(ncols=4, nrows=3,\
                         sharex=True, sharey='row',\
                         subplot_kw=dict(adjustable='box-forced'))

产生以下内容:

enter image description here

但是,

fig, axes = plt.subplots(ncols=4, nrows=3,\
                         sharex='col', sharey='row',\
                         subplot_kw=dict(adjustable='box-forced'))

有点在列之间留出一些空间,并破坏了要平方的子图的 adjustable='box-forced' 声明:

enter image description here

我想知道为什么会这样?

最佳答案

您已使用参数 sharey=True 将共享 y 轴应用于所有 子图。

sharey='row' 有一个方便的参数,它将使每一行子图共享相同的 y 轴。因此,将图形的创建更改为:

fig, axes = plt.subplots(ncols=4, nrows=3,\
                         sharex=True, sharey='row',\
                         subplot_kw=dict(adjustable='box-forced'))

这将给出下图:

enter image description here

关于matplotlib - sharey ='row' 和 sharey ='True' 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50439431/

相关文章:

python - matplotlib 运行但不生成图形

python - 在 matplotlib 中创建具有不同形状的子图

python - 如何将 ipywidget slider 的默认位置更改为 matplotlib 图的一侧?

python - matplotlib 中 Windrose 的子图

python - 将矩形/正方形区域组合成更大的区域 - imshow/python

python - 循环遍历 2d 子图,就好像它是 1-D

Matlab - 绘图窗口排列

Python:如何在函数中分别绘制不同的图?

python - 从子图元素中获取已打印标签的列表,以避免图例中的重复输入

python - 刻度线在颜色条上显示不正确