python - Pylab - 为一些子图调整 hspace

标签 python matplotlib

我有一个情节,我希望其中一个面板与其他四个面板分开。我希望其余四个面板共享 x 轴。该图如下所示。我希望底部的四个面板共享 x 轴。我试过了

f = plt.figure()
ax6=f.add_subplot(511)
ax4=f.add_subplot(515)
ax1=f.add_subplot(512,sharex=ax4)
ax2=f.add_subplot(513,sharex=ax4)
ax3=f.add_subplot(514,sharex=ax4)

但是,这对我不起作用。附图是用

制作的
f = plt.figure()
ax6=f.add_subplot(511)
ax4=f.add_subplot(515)
ax1=f.add_subplot(512)
ax2=f.add_subplot(513)
ax3=f.add_subplot(514)

然后将 xticks 设置为无

ax1.get_xaxis().set_ticklabels([])
ax2.get_xaxis().set_ticklabels([])
ax3.get_xaxis().set_ticklabels([])

使用 f.subplots_adjust(hspace=0) 连接所有子图。有没有办法只加入底部的四个面板?

谢谢! enter image description here

最佳答案

使用两个独立的 gridspec objects 是最简单的为了这。这样您就可以为不同的子图组设置独立的边距、填充等。

举个简单的例子:

import numpy as np
import matplotlib.pyplot as plt

# We'll use two separate gridspecs to have different margins, hspace, etc
gs_top = plt.GridSpec(5, 1, top=0.95)
gs_base = plt.GridSpec(5, 1, hspace=0)
fig = plt.figure()

# Top (unshared) axes
topax = fig.add_subplot(gs_top[0,:])
topax.plot(np.random.normal(0, 1, 1000).cumsum())

# The four shared axes
ax = fig.add_subplot(gs_base[1,:]) # Need to create the first one to share...
other_axes = [fig.add_subplot(gs_base[i,:], sharex=ax) for i in range(2, 5)]
bottom_axes = [ax] + other_axes

# Hide shared x-tick labels
for ax in bottom_axes[:-1]:
    plt.setp(ax.get_xticklabels(), visible=False)

# Plot variable amounts of data to demonstrate shared axes
for ax in bottom_axes:
    data = np.random.normal(0, 1, np.random.randint(10, 500)).cumsum()
    ax.plot(data)
    ax.margins(0.05)

plt.show()

enter image description here

关于python - Pylab - 为一些子图调整 hspace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28820618/

相关文章:

python - 在 Python 中从文件名中提取文件扩展名的方法是什么?

python - 在Python中绘制一个特定的函数

来自 x,y,z 数据的 matplotlib pcolormesh 图

python - 创建一个 Pandas 散点图,其中一个轴是索引

python - 有没有办法使用另一个字典在 Python 字典上设置多个默认值?

python - 在 Linux 上编译 Python 绑定(bind)

Python 字符串双重拆分?

Python:将非特定格式的字符串转换为字典?

Python ggplot 问题绘制 >8 只股票和图例被截断

python - 如何在matplotlib中将 'underbar'放在字母(下划线字符)下面?