You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matplotlib子图网格中创建极坐标仪表盘图表时尺寸异常问题求助

Matplotlib子图网格中创建极坐标仪表盘图表时尺寸异常问题求助

我正在尝试制作一个近似半圆的仪表盘图表,参考了相关教程,但我的画布是通过fig, ax = plt.subplots(2, 4, figsize=(11,6))创建的多子图网格,其他子图都是常规的笛卡尔坐标图表(比如用ax[0,0].bar()绘制的柱状图)。

当我尝试把仪表盘的极坐标代码适配到子图网格中时,比如修改为:

ax[0,0].bar(x=[0, 0.44, 0.88,1.32,1.76,2.2,2.64], width=0.5, height=0.5, bottom=2, linewidth=3, edgecolor="white", color=colors, align="edge");

发现仪表盘图表没有被限制在子图网格内,尺寸异常偏大,超出了预期的位置。我猜测问题出在教程里的ax = fig.add_subplot(projection="polar");这行代码——因为我其他的子图都没有用到极坐标投影,但我不知道该怎么给已有的子图网格中的指定位置设置极坐标投影。

附上异常效果的截图:
Image of oversized gauge chart


编辑补充:以下是带随机数据的完整可复现示例:

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(2, 2, figsize=(11,6))

gauge_colors = ["#FF2C2C", "#FFF34F", "#39FF14", "#FFF34F", "#FF2C2C"]
ax[0, 0] = plt.subplot(projection="polar")
ax[0, 0].bar(x=[0, 0.44, 0.88, 1.32, 1.76, 2.2, 2.64], width=0.5, height=0.5, bottom=2,
             linewidth=3, edgecolor="white",
             color=gauge_colors, align="edge");
ax[0, 0].annotate("50", xytext=(0, 0), xy=(1.1, 2.0),
                  arrowprops=dict(arrowstyle="wedge, tail_width=0.5", color="black", shrinkA=0),
                  bbox=dict(boxstyle="circle", facecolor="black", linewidth=2.0, ),
                  fontsize=45, color="white", ha="center"
                  );

for i in range(2):
    for j in range(2):
        if i > 0 or j > 0:
            ax[i, j].plot(np.random.rand(10))

plt.show()

备注:内容来源于stack exchange,提问作者Charlie Horn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 12:59:30