我试图绘制一个有4个子块和2个色条的图形。这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from matplotlib import rcParams
rcParams["savefig.dpi"] = 100
rcParams["font.size"] = 18
x1 = np.linspace(100, 1000, 10)
y1 = np.linspace(10, 17, 10)
z1 = np.linspace(4, 18, 10)
t1 = np.linspace(-0.3, 0.4, 10)
fig, axes = plt.subplots(2, 2, sharey = True, figsize = (10, 10))
a0 = axes[0][0].scatter(x1, y1, s = 40, c = z1, marker = 'o')
cbar1 = fig.colorbar(a0)
axes[0][0].set_ylabel('y1')
axes[0][0].set_xlabel('x1')
axes[0][0].xaxis.set_major_locator(MaxNLocator(4))
a1 = axes[0][1].scatter(t1, y1, s = 40, c = z1, marker = 'o')
axes[0][1].xaxis.set_major_locator(MaxNLocator(4))
axes[0][1].set_xlabel('t1')
cbar1.ax.set_ylabel('z1', rotation = 270)
x2 = np.linspace(450, 900, 20)
y2 = np.linspace(11, 12.5, 20)
z2 = np.linspace(12, 60, 20)
t2 = np.linspace(-0.3, 0.4, 20)
a0 = axes[1][0].scatter(x2, y2, c = z2, marker = 'o')
cbar2 = fig.colorbar(a0)
axes[1][0].set_ylabel('y2')
axes[1][0].set_xlabel('x2')
axes[1][0].xaxis.set_major_locator(MaxNLocator(4))
a1 = axes[1][1].scatter(t2, y2, c = z2, marker = 'o')
axes[1][0].xaxis.set_major_locator(MaxNLocator(4))
axes[1][1].set_xlabel('t2')
cbar2.ax.set_ylabel('z2', rotation = 270)
plt.show()
这是数字:
我想解决的问题是:
色条在最右边。我希望第一行右边有一个颜色条,第二行右边有另一个颜色条(基本上,它就是这样)。
我该怎么做?谢谢!
最佳答案
您可以输入另一个参数来选择要在哪个轴上打印颜色栏。
下面是您的代码更改:
cbar1 = fig.colorbar(a0, ax=axes[0][1])
cbar2 = fig.colorbar(a0, ax=axes[1][1])
产生这个情节的:
关于python - 具有多个颜色条的子图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33198882/