Python 调整gridspec单元格内matplotlib对象的大小(matshow和颜色栏大小不匹配)
matshow和colorbar对象在gridspec单元格内不填充相同的空间,因此它们的高度不同 通常我会使用colorbar“收缩”参数,但当嵌套在gridspec对象中时,这似乎不起作用 如何在不调整matshow热图大小的情况下收缩colorbar对象 提前谢谢Python 调整gridspec单元格内matplotlib对象的大小(matshow和颜色栏大小不匹配),python,matplotlib,colorbar,imshow,Python,Matplotlib,Colorbar,Imshow,matshow和colorbar对象在gridspec单元格内不填充相同的空间,因此它们的高度不同 通常我会使用colorbar“收缩”参数,但当嵌套在gridspec对象中时,这似乎不起作用 如何在不调整matshow热图大小的情况下收缩colorbar对象 提前谢谢 import numpy as np import pandas as pd import matplotlib.pyplot as plt from matplotlib import gridspec df = pd.Da
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import gridspec
df = pd.DataFrame((np.random.randint(0, 3, 10000).reshape(100, 100)))
fig = plt.figure(figsize=(15,15))
gs = gridspec.GridSpec(10, 10)
#### other axes removed for simplicity
ax2 = fig.add_subplot(gs[2:,:8])
# plot heatmap
cax = ax2.matshow(df, interpolation='nearest', cmap=plt.cm.YlGn, aspect='equal')
ax2.set_xticks([])
ax2.set_yticks([])
ax3 = fig.add_subplot(gs[2:,8])
fig.colorbar(cax, cax=ax3)
plt.tight_layout()
gs.update(wspace=2, hspace=0.1)
plt.show()
编辑:带注释的图像以进行澄清
您可以使用matplotlib。下面是使用您问题中的数据的示例:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import gridspec
fig = plt.figure(figsize=(15,15))
df = pd.DataFrame((np.random.randint(0, 3, 10000).reshape(100, 100)))
gs = gridspec.GridSpec(10, 10)
ax2 = fig.add_subplot(gs[2:,:8])
im = ax2.matshow(df,interpolation='nearest',cmap=plt.cm.YlGn, aspect='equal')
divider = make_axes_locatable(ax2)
cax = divider.append_axes("right", size="5%", pad=0.05)
plt.colorbar(im, cax=cax)
plt.show()
这将生成以下图表,在我看来,它们的大小相同:
您可以使用matplotlib。下面是使用您问题中的数据的示例:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import gridspec
fig = plt.figure(figsize=(15,15))
df = pd.DataFrame((np.random.randint(0, 3, 10000).reshape(100, 100)))
gs = gridspec.GridSpec(10, 10)
ax2 = fig.add_subplot(gs[2:,:8])
im = ax2.matshow(df,interpolation='nearest',cmap=plt.cm.YlGn, aspect='equal')
divider = make_axes_locatable(ax2)
cax = divider.append_axes("right", size="5%", pad=0.05)
plt.colorbar(im, cax=cax)
plt.show()
这将生成以下图表,在我看来,它们的大小相同:
热图和色条“高度不同”。在你所附的图片中,他们看起来和我一样高。你能澄清一下你的意思吗?他们非常接近,但不完全一样!我已经添加了另一张图片来澄清热图和色条“高度不同”。在你所附的图片中,他们看起来和我一样高。你能澄清一下你的意思吗?他们非常接近,但不完全一样!我已经添加了另一个图像,以澄清非常感谢!正是我需要的汉克斯!正是我需要的