python - matplotlib:减少子图中的轴宽度

标签 python matplotlib bar-chart colorbar colormap

我有一个 matplotlib 条形图,该条形图根据某些规则通过颜色图进行着色。我需要在主轴右侧有一个颜色条,因此我添加了一个新轴

fig, (ax, ax_cbar) = plt.subplots(1,2)

并设法在 ax_bar 轴中绘制颜色条,同时将数据显示在 ax 轴中。现在我需要减小 ax_bar 的宽度,因为它看起来像这样: enter image description here

我该怎么办?

最佳答案

使用子图总是会平均划分你的图形。您可以通过多种方式手动划分图形。我的首选方法是使用 subplot2grid。

在此示例中,我们将图形设置为 1 行 10 列。然后,我们将 ax 设置为从 row,column = (0,0) 开始,宽度为 9 列。然后将 ax_cbar 设置为从 (0,9) 开始,默认宽度为 1 列。

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(8,6))

num_columns = 10
ax = plt.subplot2grid((1,num_columns), (0,0), colspan=num_columns-1)
ax_cbar = plt.subplot2grid((1,num_columns), (0,num_columns-1))

关于python - matplotlib:减少子图中的轴宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44002453/

相关文章:

python - 无法使用python将数据插入sqlite3

python - 使用 Anaconda 或 Canopy 安装 Python 模块

python - 我想在 plt 中显示图像,但有些困惑

python - matplotlib 中每个图的宽度和高度轴相同

python 局部作用域

python - 如何为每列使用不同的默认值初始化 NumPy 数组?

python - 单个图上的多个条形 (Python)

python - 无法增加 Bokeh 中整个图表的大小并使图表处于水平轴

javascript - 排序水平条形图 D3js

javascript - jQuery float 条形数字 : Numbers positioning