我正在尝试按照 本教程 使用 matplotlib 创建一个大致半圆形的仪表图表。然而,我的图包含几个不同的图表 - 它是通过如下线创建的:
fig, ax = plt.subplots(2, 4, figsize=(11,6))
创建的每个图表都与此类似:
ax[0,0].bar(categories, values)
我遇到的问题是,如果我尝试将教程代码更改为类似的内容
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");
线的问题,因为我创建的其他图表都没有使用类似的东西,但我不确定如何指定图表需要使用极坐标。欢迎任何建议。编辑:这是一个带有随机数据的完整可重现示例
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()
如果您希望某些子图采用笛卡尔坐标,而其他子图采用极坐标,一种解决方案是一次添加一个子图,因为您可以在其中选择坐标系:
fig = plt.figure()
ax1 = fig.add_subplot(221, projection='polar')
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
gauge_colors = ["#FF2C2C", "#FFF34F", "#39FF14", "#FFF34F", "#FF2C2C"]
ax1.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");
ax1.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=12, color="white", ha="center"
);
allaxes = [ax1, ax2, ax3, ax4]
for i in range(4):
if i > 0:
allaxes[i].plot(np.random.rand(10))
plt.show()