使用共享 x 轴合并 matplotlib 子图

Posted

技术标签:

【中文标题】使用共享 x 轴合并 matplotlib 子图【英文标题】:Merge matplotlib subplots with shared x-axis 【发布时间】:2016-10-10 18:57:32 【问题描述】:

我有两个图表,它们都具有相同的 x 轴,但具有不同的 y 轴缩放比例。

带有规则轴的图是带有描绘衰减趋势线的数据,而 y 半对数缩放描绘了拟合的准确性。

fig1 = plt.figure(figsize=(15,6))
ax1 = fig1.add_subplot(111)

# Plot of the decay model 
ax1.plot(FreqTime1,DecayCount1, '.', color='mediumaquamarine')

# Plot of the optimized fit
ax1.plot(x1, y1M, '-k', label='Fitting Function: $f(t) = %.3f e^%.3f\t \
         %+.3f$' % (aR1,kR1,bR1))

ax1.set_xlabel('Time (sec)')
ax1.set_ylabel('Count')
ax1.set_title('Run 1 of Cesium-137 Decay')

# Allows me to change scales
# ax1.set_yscale('log')
ax1.legend(bbox_to_anchor=(1.0, 1.0), prop='size':15, fancybox=True, shadow=True)

现在,我正在尝试像此链接提供的示例一样将两者紧密结合在一起 http://matplotlib.org/examples/pylab_examples/subplots_demo.html

尤其是这个

在查看示例代码时,我对如何植入 3 件事有点困惑:

1) 以不同方式缩放轴

2) 保持指数衰减图的图形大小相同,但折线图的 y 大小和 x 大小相同。

例如:

3) 保持函数的标签只出现在衰减图中。

任何帮助将不胜感激。

【问题讨论】:

【参考方案1】:

看里面的代码和cmets:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import gridspec

# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

fig = plt.figure()
# set height ratios for subplots
gs = gridspec.GridSpec(2, 1, height_ratios=[2, 1]) 

# the first subplot
ax0 = plt.subplot(gs[0])
# log scale for axis Y of the first subplot
ax0.set_yscale("log")
line0, = ax0.plot(x, y, color='r')

# the second subplot
# shared axis X
ax1 = plt.subplot(gs[1], sharex = ax0)
line1, = ax1.plot(x, y, color='b', linestyle='--')
plt.setp(ax0.get_xticklabels(), visible=False)
# remove last tick label for the second subplot
yticks = ax1.yaxis.get_major_ticks()
yticks[-1].label1.set_visible(False)

# put legend on first subplot
ax0.legend((line0, line1), ('red line', 'blue line'), loc='lower left')

# remove vertical gap between subplots
plt.subplots_adjust(hspace=.0)
plt.show()

【讨论】:

@Serenity 很好的答案!但是如果我使用fig, axis = plt.subplots(nrows=4) 创建我的轴数组,是否可以共享 x 轴? 是的,有可能,为什么不呢? @Serenity 好吧,我认为如果使用plt.plot() 是可能的,那么我可以指定sharex。但是在Pandas 中,我无法触及底层sharex,我必须为DataFrame.plot() 创建一个新轴。这就是为什么我喜欢你的回答! 请注意,可以在一次调用中创建两个轴:fig, (ax0, ax1) = plt.subplots(2,1, sharex=True, gridspec_kw=dict(height_ratios=[2, 1]))【参考方案2】:

这是我的解决方案:

import numpy as np
import matplotlib.pyplot as plt

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

fig, (ax1,ax2) = plt.subplots(nrows=2, sharex=True, subplot_kw=dict(frameon=False)) # frameon=False removes frames

plt.subplots_adjust(hspace=.0)
ax1.grid()
ax2.grid()

ax1.plot(x, y, color='r')
ax2.plot(x, y, color='b', linestyle='--')

另一个选项是seaborn.FacetGrid,但这需要 Seaborn 和 Pandas 库。

【讨论】:

【参考方案3】:

这里有一些改编,展示了在绘制 pandas 数据框时代码如何添加组合图例。 ax=ax0 可用于在给定的ax 上绘图,ax0.get_legend_handles_labels() 获取图例的信息。

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

dates = pd.date_range('20210101', periods=100, freq='D')
df0 = pd.DataFrame('x': np.random.normal(0.1, 1, 100).cumsum(),
                    'y': np.random.normal(0.3, 1, 100).cumsum(), index=dates)
df1 = pd.DataFrame('z': np.random.normal(0.2, 1, 100).cumsum(), index=dates)

fig, (ax0, ax1) = plt.subplots(nrows=2, sharex=True, gridspec_kw='height_ratios': [2, 1], 'hspace': 0)

df0.plot(ax=ax0, color=['dodgerblue', 'crimson'], legend=False)
df1.plot(ax=ax1, color='limegreen', legend=False)

# put legend on first subplot
handles0, labels0 = ax0.get_legend_handles_labels()
handles1, labels1 = ax1.get_legend_handles_labels()
ax0.legend(handles=handles0 + handles1, labels=labels0 + labels1)

# remove last tick label for the second subplot
yticks = ax1.get_yticklabels()
yticks[-1].set_visible(False)

plt.tight_layout()
plt.show()

【讨论】:

以上是关于使用共享 x 轴合并 matplotlib 子图的主要内容,如果未能解决你的问题,请参考以下文章

python使用matplotlib可视化subplots子图subplots绘制子图并为可视化的多个子图设置共享的Y轴

Python seaborn可视化:组合多个seaborn可视化结果并使得组合结果图像共享Y轴使用matplotlib的subplots子图函数的gridspec_kw参数指定子图的相对大小或者比率

如何在 matplotlib 子图图像轴之间强制相同大小

Matplotlib 在所有子图上显示 x-ticks 和唯一的 y 标签

无法隐藏子图轴标签或在matplotlib中设置MaxNLocator

Matplotlib:每个时间序列子图绘制多条线