【发布时间】:2019-08-19 18:28:32
【问题描述】:
在我开始之前,我想说我已经尝试关注 this 和 this 在同一问题上的帖子,但是他们使用 imshow 热图进行操作,这与我正在做的 2d 直方图不同。
这是我的代码(实际数据已替换为随机生成的数据,但要点相同):
import matplotlib.pyplot as plt
import numpy as np
def subplots_hist_2d(x_data, y_data, x_labels, y_labels, titles):
fig, a = plt.subplots(2, 2)
a = a.ravel()
for idx, ax in enumerate(a):
image = ax.hist2d(x_data[idx], y_data[idx], bins=50, range=[[-2, 2],[-2, 2]])
ax.set_title(titles[idx], fontsize=12)
ax.set_xlabel(x_labels[idx])
ax.set_ylabel(y_labels[idx])
ax.set_aspect("equal")
cb = fig.colorbar(image[idx])
cb.set_label("Intensity", rotation=270)
# pad = how big overall pic is
# w_pad = how separate they're left to right
# h_pad = how separate they're top to bottom
plt.tight_layout(pad=-1, w_pad=-10, h_pad=0.5)
x1, y1 = np.random.uniform(-2, 2, 10000), np.random.uniform(-2, 2, 10000)
x2, y2 = np.random.uniform(-2, 2, 10000), np.random.uniform(-2, 2, 10000)
x3, y3 = np.random.uniform(-2, 2, 10000), np.random.uniform(-2, 2, 10000)
x4, y4 = np.random.uniform(-2, 2, 10000), np.random.uniform(-2, 2, 10000)
x_data = [x1, x2, x3, x4]
y_data = [y1, y2, y3, y4]
x_labels = ["x1", "x2", "x3", "x4"]
y_labels = ["y1", "y2", "y3", "y4"]
titles = ["1", "2", "3", "4"]
subplots_hist_2d(x_data, y_data, x_labels, y_labels, titles)
这就是它正在生成的:
所以现在我的问题是我无法让颜色条适用于所有 4 个直方图。同样出于某种原因,与其他直方图相比,右下角直方图的行为似乎很奇怪。在我发布的链接中,他们的方法似乎没有使用a = a.ravel(),我只在这里使用它,因为这是让我将 4 个直方图绘制为子图的唯一方法。帮助?
编辑:
Thomas Kuhn 您的新方法实际上解决了我所有的问题,直到我放下标签并尝试使用plt.tight_layout() 来解决重叠问题。似乎如果我在plt.tight_layout(pad=i, w_pad=0, h_pad=0) 中输入特定参数,那么颜色条就会开始出现异常。我现在将解释我的问题。
我已经对你的新方法进行了一些更改,使其适合我想要的,就像这样
def test_hist_2d(x_data, y_data, x_labels, y_labels, titles):
nrows, ncols = 2, 2
fig, axes = plt.subplots(nrows, ncols, sharex=True, sharey=True)
##produce the actual data and compute the histograms
mappables=[]
for (i, j), ax in np.ndenumerate(axes):
H, xedges, yedges = np.histogram2d(x_data[i][j], y_data[i][j], bins=50, range=[[-2, 2],[-2, 2]])
ax.set_title(titles[i][j], fontsize=12)
ax.set_xlabel(x_labels[i][j])
ax.set_ylabel(y_labels[i][j])
ax.set_aspect("equal")
mappables.append(H)
##the min and max values of all histograms
vmin = np.min(mappables)
vmax = np.max(mappables)
##second loop for visualisation
for ax, H in zip(axes.ravel(), mappables):
im = ax.imshow(H,vmin=vmin, vmax=vmax, extent=[-2,2,-2,2])
##colorbar using solution from linked question
fig.colorbar(im,ax=axes.ravel())
plt.show()
# plt.tight_layout
# plt.tight_layout(pad=i, w_pad=0, h_pad=0)
现在如果我尝试生成我的数据,在这种情况下:
phi, cos_theta = get_angles(runs)
detector_x1, detector_y1, smeared_x1, smeared_y1 = detection_vectorised(1.5, cos_theta, phi)
detector_x2, detector_y2, smeared_x2, smeared_y2 = detection_vectorised(1, cos_theta, phi)
detector_x3, detector_y3, smeared_x3, smeared_y3 = detection_vectorised(0.5, cos_theta, phi)
detector_x4, detector_y4, smeared_x4, smeared_y4 = detection_vectorised(0, cos_theta, phi)
这里detector_x, detector_y, smeared_x, smeared_y是所有数据点列表
所以现在我将它们放入2x2 列表中,以便它们可以通过我的绘图函数适当地解包,如下所示:
data_x = [[detector_x1, detector_x2], [detector_x3, detector_x4]]
data_y = [[detector_y1, detector_y2], [detector_y3, detector_y4]]
x_labels = [["x positions(m)", "x positions(m)"], ["x positions(m)", "x positions(m)"]]
y_labels = [["y positions(m)", "y positions(m)"], ["y positions(m)", "y positions(m)"]]
titles = [["0.5m from detector", "1.0m from detector"], ["1.5m from detector", "2.0m from detector"]]
我现在运行我的代码
test_hist_2d(data_x, data_y, x_labels, y_labels, titles)
只打开plt.show(),它会给出这个:
这很棒,因为在数据和视觉方面,这正是我想要的,即颜色图对应于所有 4 个直方图。但是,由于标签与标题重叠,我想我会运行相同的东西,但这次使用plt.tight_layout(pad=a, w_pad=b, h_pad=c) 希望我能够调整重叠标签问题。但是这一次我如何更改数字 a, b 和 c 并不重要,我总是让我的颜色条位于图表的第二列,如下所示:
现在更改a 只会使整个子图变大或变小,我能做的最好的就是用plt.tight_layout(pad=-10, w_pad=-15, h_pad=0) 调整它,看起来像这样
看来无论你的新方法在做什么,它都让整个情节失去了可调整性。你的解决方案在解决一个问题方面同样出色,反过来又创造了另一个问题。那么在这里做什么最好呢?
编辑 2:
将 fig, axes = plt.subplots(nrows, ncols, sharex=True, sharey=True, constrained_layout=True) 与plt.show() gives 一起使用
如您所见,子图的列之间仍然存在垂直间隙,即使使用 plt.subplots_adjust() 也无法消除。
【问题讨论】:
-
如果你有足够新的 matplotlib colorbar(image[idx], ax=a) 应该可以工作。
-
@ThomasKühn 我不确定您是否阅读了我的问题。我特别说过,您刚刚发布的链接中的帖子,我已经尝试了他们的方法,但它们对我不起作用。
-
对不起,我显然准备得不够彻底。
-
您的新问题似乎与直方图无关;像往常一样minimal reproducible examples 会有所帮助。该问题可能会在较新版本的 matplotlib 中得到解决,因此报告您的 matplotlib 版本至关重要。另外,你可以试试
constrained_layout;这是tight_layout的替代品,在某些情况下效果更好。 -
好的,非常感谢!
标签: python-3.x matplotlib subplot colorbar