python - 如何使用 gridspec 调整列宽?

标签 python matplotlib

我该如何修复这个情节?

我想要:

  1. 两个颜色条不重叠。
  2. 使它们的高度等于绘图。

这是我的代码:

combined = (...) # some irrelevant to question data praparation - this variable contain square matrix with RGBA chanels

plt.figure(dpi=300, figsize=(2.1,1.9))
gs = gridspec.GridSpec(1, 3, width_ratios=[20,1,1])
ax1 = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])
ax3 = plt.subplot(gs[2])

cax = ax1.imshow(combined, interpolation="None")
mpl.colorbar.ColorbarBase(ax2, cmap=cmap_top, norm=norm_top)
mpl.colorbar.ColorbarBase(ax3, cmap=cmap_top, norm=norm_top)

我使用的是 python 3.6 和 matplotlib 2.0。

broken plot

最佳答案

对于问题 1,我会考虑两种可能性: A。修改控制图形之间水平间距的 wspace 参数,即:

gs = gridspec.GridSpec(1, 3, width_ratios=[20,1,1])
gs.update(wspace=0.05)

b.在第一个和第二个颜色条之间添加一个额外的列,充当一些空白空间:

gs = gridspec.GridSpec(1, 4, width_ratios=[20,1,0.15,1])

对于问题 2,我会用不同的方式写:

ax2=plt.subplot(gs[0,1]  )
cb1 = matplotlib.colorbar.ColorbarBase(ax2, cmap="RdBu_r")

希望对你有帮助!

关于python - 如何使用 gridspec 调整列宽?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43804513/

相关文章:

python - 我需要导入什么才能访问我的模型?

python - 如何在 wxPython 中使用 matplotlib 事件处理?

python - 从 (x, y, z) 数据集在 matplotlib 中绘制等值线/等高线

python - pip - 在 vi​​rtualenv 中安装 matplotlib

python - py2exe:排除要导入其所有部分的包的部分

python - 在位置参数和选项之间共享目标

python - 带有方程式和 R2 文本的 Seaborn lmplot

python - Matplotlib 在绘图中写入 '±'

python - 从 multiprocessing.Queue 获得近后进先出行为的干净方法? (甚至只是*不*接近先进先出)

python - 来自 Python/SQLAlchemy 的 SQL Server 查询中需要单个反斜杠