gpt4 book ai didi

python - 如何在多图中使用颜色条调整图的宽度?

转载 作者:行者123 更新时间:2023-12-03 03:03:40 25 4
gpt4 key购买 nike

我在多重绘图中遇到颜色条问题。当我添加颜色条时,这两个图没有对齐。如果我删除颜色条,绘图就会很好地对齐。

如何调整多图中的绘图大小?我只想调整下图或上图的宽度以对齐图。这是我的代码。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.gridspec as gridspec

fig = plt.subplots(2,figsize=(5, 8))
gs = gridspec.GridSpec(2, 1, height_ratios=[ 3,2],hspace=0.0)

ax1 = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])

xx = np.linspace(0,5,50)
yy = np.linspace(0,5,50)
X, Y = np.meshgrid(xx, yy)
T = X**2

norm = mpl.colors.Normalize(vmin=0,vmax=10)
pcm=ax1.pcolormesh(X,Y,T, norm =norm)
plt.colorbar(pcm, orientation="vertical", ax =ax1)

ax2.plot(xx, xx**2)

结果如下。

the result

最佳答案

您也可以尝试水平条方向。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.gridspec as gridspec

fig = plt.subplots(2,figsize=(5, 8))
gs = gridspec.GridSpec(2, 1, height_ratios=[ 3,2],hspace=0.0)

ax1 = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])

xx = np.linspace(0,5,50)
yy = np.linspace(0,5,50)
X, Y = np.meshgrid(xx, yy)
T = X**2

norm = mpl.colors.Normalize(vmin=0,vmax=10)
pcm=ax1.pcolormesh(X,Y,T, norm =norm)
plt.colorbar(pcm, orientation="horizontal", ax =ax1, pad=0.1)

ax2.plot(xx, xx**2)

enter image description here

关于python - 如何在多图中使用颜色条调整图的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60033754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com