PYPLOT:Rescale GridSpec将包括配色杆
我正在尝试将GridSpec子图添加到Pyplot中的2D直方图中。我希望直方图具有配色栏。但是,当我添加配色栏时,X轴不再匹配。
如何使轴匹配?
我遇到的问题的一个示例在下面复制。由于配色栏,X轴不再匹配:
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
from scipy.stats import binned_statistic, sem
import numpy as np
mean = [0, 0]
cov = [[1, 0.5], [0.5, 1]]
x, y = np.random.multivariate_normal(mean, cov, 5000).T
fig=plt.figure(figsize=(10,10))
gs = gridspec.GridSpec(2, 1, height_ratios=[2, 1])
ax0 = plt.subplot(gs[0])
h = ax.hist2d(x,
y,
density=True,
cmap="plasma",
norm=LogNorm(),
bins=100)
x_min = -4
x_max = 4
x_rng = np.linspace(x_min, x_max, 100)
h = ax0.hist2d(x,
y,
density=True,
cmap="plasma",
norm=LogNorm(),
bins=50)
ax0.plot(x_rng, x_rng, color="purple", linestyle="--", lw=3, label="1:1")
ax0.set_ylabel("y", fontsize=20)
ax0.set_ylim([x_min-0.5, x_max+0.5])
ax0.set_xlim([x_min-0.5, x_max+0.5])
cb = plt.colorbar(h[3],ax=ax0, pad = .015, aspect=50)
cb.set_label('Probability Density [unit$^{-2}$]',size=20)
cb.ax.tick_params(labelsize=20)
ax0.tick_params(axis="y", labelsize=20)
ax0.legend(fontsize=15)
ax1 = plt.subplot(gs[1], sharex = ax, aspect="auto")
y_mean, bins, _ = binned_statistic(x,
y,
statistic=np.mean,
bins = x_rng)
y_sem, bins, _ = binned_statistic(x,
y,
statistic=sem,
bins = x_rng)
x_rng_bcs = (bins[:-1] + bins[1:])/2.
ax1.errorbar(x_rng_bcs, y_mean - x_rng_bcs, yerr = y_sem, color="purple", fmt="o")
ax1.errorbar(x_rng_bcs, np.zeros_like(x_rng_bcs), color="purple", linestyle="--")
ax1.tick_params(axis="x", labelsize=20, rotation=45)
ax1.tick_params(axis="y", labelsize=20, rotation=45)
ax1.set_ylabel("$\Delta y$", fontsize=20)
ax1.set_xlabel("x", fontsize=20)
fig.tight_layout()
fig.subplots_adjust(hspace=.0)
fig.show()
I am trying to add a GridSpec subplot to a 2D histogram in PyPlot. I want the histogram to have a colorbar. However, when I add the colorbar, the x-axes no longer match up.
How can I make the axes match?
An example of the problem I am having is replicated below. Because of the colorbar, the x-axes no longer match:
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
from scipy.stats import binned_statistic, sem
import numpy as np
mean = [0, 0]
cov = [[1, 0.5], [0.5, 1]]
x, y = np.random.multivariate_normal(mean, cov, 5000).T
fig=plt.figure(figsize=(10,10))
gs = gridspec.GridSpec(2, 1, height_ratios=[2, 1])
ax0 = plt.subplot(gs[0])
h = ax.hist2d(x,
y,
density=True,
cmap="plasma",
norm=LogNorm(),
bins=100)
x_min = -4
x_max = 4
x_rng = np.linspace(x_min, x_max, 100)
h = ax0.hist2d(x,
y,
density=True,
cmap="plasma",
norm=LogNorm(),
bins=50)
ax0.plot(x_rng, x_rng, color="purple", linestyle="--", lw=3, label="1:1")
ax0.set_ylabel("y", fontsize=20)
ax0.set_ylim([x_min-0.5, x_max+0.5])
ax0.set_xlim([x_min-0.5, x_max+0.5])
cb = plt.colorbar(h[3],ax=ax0, pad = .015, aspect=50)
cb.set_label('Probability Density [unit$^{-2}$]',size=20)
cb.ax.tick_params(labelsize=20)
ax0.tick_params(axis="y", labelsize=20)
ax0.legend(fontsize=15)
ax1 = plt.subplot(gs[1], sharex = ax, aspect="auto")
y_mean, bins, _ = binned_statistic(x,
y,
statistic=np.mean,
bins = x_rng)
y_sem, bins, _ = binned_statistic(x,
y,
statistic=sem,
bins = x_rng)
x_rng_bcs = (bins[:-1] + bins[1:])/2.
ax1.errorbar(x_rng_bcs, y_mean - x_rng_bcs, yerr = y_sem, color="purple", fmt="o")
ax1.errorbar(x_rng_bcs, np.zeros_like(x_rng_bcs), color="purple", linestyle="--")
ax1.tick_params(axis="x", labelsize=20, rotation=45)
ax1.tick_params(axis="y", labelsize=20, rotation=45)
ax1.set_ylabel("$\Delta yquot;, fontsize=20)
ax1.set_xlabel("x", fontsize=20)
fig.tight_layout()
fig.subplots_adjust(hspace=.0)
fig.show()
Which yields:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种选项是为您的配色键制作一组单独的轴,并使用
width_ratios
使其大小合适。例如,替换
为
cax
关键字将所需的轴替换为以下图,您可以调整
width_ratios
以获取所需配色栏宽度。下面的完整代码
One option is to make a separate set of axes for you colorbar and use
width_ratios
to make it the right size.E.g. replace
with
and then when you plot the colorbar pass the required axes as the
cax
keywordDoing this will give the following plot, and you can adjust the
width_ratios
to get the desired colorbar width.Full code below
这就是约束_layout应该做的:
This is what constrained_layout is supposed to do: