绘制多个子图需要一个颜色

2024-10-06 12:37:49 发布

您现在位置:Python中文网/ 问答频道 /正文

每个子批次我都有多个colobar,我想要一个。在

for i in range(6):
     plot.subplot(2,3,i)
     im=plot.contourf(xlon[:],xlat[:],rain[i,:,:])
     plot.colorbar(im)
plot.show()

Tags: inforplotshowrangeimrainsubplot
1条回答
网友
1楼 · 发布于 2024-10-06 12:37:49

您可以通过在其自己的轴上添加colorbar来完成此操作。这可以通过手动创建一个附加轴并根据需要使用subplots_adjust()add_axes()移动现有绘图来完成

import matplotlib.pyplot as plt
import numpy as np
import random

fig, ax = plt.subplots(figsize=(10, 6), dpi=300)
for i in range(1,7):
    # This simply creates some random data to populate with
    a = np.arange(10)
    x, y = np.meshgrid(a, a)
    z = np.random.randint(0, 7, (10, 10))

    plt.subplot(2,3,i)
    im=plt.contourf(x, y, z)

# Tight layout is optional
fig.tight_layout()
fig.subplots_adjust(right=0.825)
cax = fig.add_axes([0.85, 0.06, 0.035, 0.91])
fig.colorbar(im, cax=cax)
plt.show()

在本例中,add_axes()的参数是[left, bottom, width, height]。这会产生类似

Sample figure with 6 subplots sharing one colorbar

编辑

要删除图间坐标轴标签、刻度线等,需要对上面的方法进行一些不平凡的修改,其中plt.subplots()用于填充子图对象的2x3数组,然后我们对其进行迭代。E、 g

^{pr2}$

Modified figure

相关问题 更多 >