python - Matplotlib:subplot2grid 中图外的颜色条

标签 python matplotlib subplot colorbar

我试图在一些子图中绘制两个高斯分布。这是结果: First result

这是代码

import numpy as np
import numpy.random as rnd
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable



mu_x1 = 3
sig_x1 = 2

mu_x2 = 3
sig_x2 = 0.5

N = int(1e3)

gaussian1 = rnd.normal(mu_x1, sig_x1, N)
gaussian2 = rnd.normal(mu_x2, sig_x2, N)
fig = plt.figure()

ax1 = plt.subplot2grid(shape = (3, 3),
                       loc = (1, 0),
                       rowspan = 2,
                       colspan = 2,
                       fig = fig
                       )
hist2d = ax1.hist2d(gaussian1,
                    gaussian2,
                    bins = 100,
                    density = True,
                    cmap=plt.cm.jet
                    )
fig.colorbar(hist2d[3], ax=ax1)
ax1.set_xlabel("$x_1$")
ax1.set_ylabel("$x_2$")



ax2 = plt.subplot2grid(shape = (3, 3),
                       loc = (0, 0),
                       rowspan = 1,
                       colspan = 2,
                       fig = fig
                       )
ax2.hist(gaussian1,
         bins = 100
         )
ax2.xaxis.set_ticks_position('top')
ax2.set_xlabel("$x_1$")
ax2.xaxis.set_label_position('top')
ax2.set_ylabel("Cuentas")

ax3 = plt.subplot2grid(shape = (3, 3),
                       loc = (1, 2),
                       rowspan = 2,
                       colspan = 1,
                       fig = fig
                       )
ax3.hist(gaussian2,
         bins = 100,
         orientation=u'horizontal')
ax3.yaxis.set_ticks_position('right')
ax3.yaxis.set_label_position('right')
ax3.set_xlabel("Cuentas")
ax3.set_ylabel("$x_2$")

plt.show()

看起来不错,但不是我想要的。我希望中间子图的 x 轴与上面子图的 x 轴一样大,因此它们重合。问题是颜色条。我想将颜色条移到中间子图之外以执行我想要的操作。我查看了一些信息,结果是:

Second result

这里是代码

fig = plt.figure()

ax1 = plt.subplot2grid(shape = (4, 3),
                       loc = (1, 0),
                       rowspan = 2,
                       colspan = 2,
                       fig = fig
                       )
hist2d = ax1.hist2d(gaussian1,
                    gaussian2,
                    bins = 100,
                    density = True,
                    cmap=plt.cm.jet
                    )
#fig.colorbar(hist2d[3], ax=ax1)
ax4 = plt.subplot2grid(shape = (4, 3),
                       loc = (3, 0),
                       rowspan = 1,
                       colspan = 2,
                       fig = fig
                       )    
fig.colorbar(hist2d[3], cax=ax4, orientation="horizontal")
ax1.set_xlabel("$x_1$")
ax1.set_ylabel("$x_2$")
ax1.axhline(mu_x2 + sig_x2*n2,c="k")
ax1.axhline(mu_x2 - sig_x2*n2,c="k")
ax1.axvline(mu_x1 + sig_x1*n1,c="k")
ax1.axvline(mu_x1 - sig_x1*n1,c="k")



ax2 = plt.subplot2grid(shape = (4, 3),
                       loc = (0, 0),
                       rowspan = 1,
                       colspan = 2,
                       fig = fig
                       )
ax2.hist(gaussian1,
         bins = 100
         )
ax2.xaxis.set_ticks_position('top')
ax2.set_xlabel("$x_1$")
ax2.xaxis.set_label_position('top')
ax2.set_ylabel("Cuentas")
ax2.axvline(mu_x1 + sig_x1*n1,c="k")
ax2.axvline(mu_x1 - sig_x1*n1,c="k")
#ax2.set_xticks([])

ax3 = plt.subplot2grid(shape = (4, 3),
                       loc = (1, 2),
                       rowspan = 2,
                       colspan = 1,
                       fig = fig
                       )
ax3.hist(gaussian2,
         bins = 100,
         orientation=u'horizontal')
ax3.yaxis.set_ticks_position('right')
ax3.yaxis.set_label_position('right')
ax3.set_xlabel("Cuentas")
ax3.set_ylabel("$x_2$")
ax3.axhline(mu_x2 + sig_x2*n2,c="k")
ax3.axhline(mu_x2 - sig_x2*n2,c="k")

现在的问题是颜色条填充了整个子图,它太大并且隐藏了上面子图的刻度。

有办法解决吗?

最佳答案

我也遇到了同样的问题。以下是我如何使用 answer 中的 add_axes 函数来管理它.

import numpy as np
import numpy.random as rnd
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable



mu_x1 = 3
sig_x1 = 2

mu_x2 = 3
sig_x2 = 0.5

N = int(1e3)

gaussian1 = rnd.normal(mu_x1, sig_x1, N)
gaussian2 = rnd.normal(mu_x2, sig_x2, N)
fig = plt.figure(figsize=(10,10))

ax1 = plt.subplot2grid(shape = (3, 3),
                       loc = (1, 0),
                       rowspan = 2,
                       colspan = 2,
                       fig = fig
                       )
hist2d = ax1.hist2d(gaussian1,
                    gaussian2,
                    bins = 100,
                    density = True,
                    cmap=plt.cm.jet
                    )
#fig.colorbar(hist2d[3], ax=ax1)
# ax4 = plt.subplot2grid(shape = (4, 3),
#                        loc = (3, 0),
#                        rowspan = 1,
#                        colspan = 2,
#                        fig = fig
#                        )    
# fig.colorbar(hist2d[3], cax=ax4, orientation="horizontal")
ax1.set_xlabel("$x_1$")
ax1.set_ylabel("$x_2$")

w=0.5
h=0.03

w1=w*(ax1.get_position().x1-ax1.get_position().x0)
x1=ax1.get_position().x0+w1/2
y1=ax1.get_position().y0-5*h

cax = fig.add_axes([x1,y1,w1,h])

plt.colorbar(hist2d[3],orientation='horizontal',cax=cax,aspect=10,shrink=0.5)



ax2 = plt.subplot2grid(shape = (3, 3),
                       loc = (0, 0),
                       rowspan = 1,
                       colspan = 2,
                       fig = fig
                       )
ax2.hist(gaussian1,
         bins = 100
         )
ax2.xaxis.set_ticks_position('top')
ax2.set_xlabel("$x_1$")
ax2.xaxis.set_label_position('top')
ax2.set_ylabel("Cuentas")
#ax2.set_xticks([])

ax3 = plt.subplot2grid(shape = (3, 3),
                       loc = (1, 2),
                       rowspan = 2,
                       colspan = 1,
                       fig = fig
                       )
ax3.hist(gaussian2,
         bins = 100,
         orientation=u'horizontal')
ax3.yaxis.set_ticks_position('right')
ax3.yaxis.set_label_position('right')
ax3.set_xlabel("Cuentas")
ax3.set_ylabel("$x_2$")

enter image description here

关于python - Matplotlib:subplot2grid 中图外的颜色条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69841872/

相关文章:

python - __init__ 的目的

python - SQLite:使用更新将特定于值的行从一个表复制到另一个表

matplotlib - 如何在单个 IPython 笔记本中多次显示相同的 matplotlib 图?

r - 在 plotly::subplot 中显示所有 plotly 标题

python - plotly,python,在其他子图上绘制直方图?

python - Chrome 不呈现由 Python/WebApp2 提供的样式表

python matplotlib条形宽度不可控

python从迭代返回数组

python - 如何使用 matplotlib 子图进行多行布局

python - 如何将字符串用于图标位图?