【发布时间】:2017-08-12 09:47:30
【问题描述】:
我想绘制一张表格,我真的很喜欢 seaborn。所以我写了一个简单的函数,它返回一个 seaborn 热图并用一些灰色填充单元格。 我的问题是,当我在子图中使用该表时,沿垂直轴的其他图会缩小到我在其上绘制 seaborn 热图的轴的宽度。
import seaborn as sns
from matplotlib.colors import ListedColormap
import numpy as np
import pandas as pd
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
def plotTable(tableDf, ax=None):
s = len(tableDf), len(tableDf.columns)
dataZeros = np.zeros(s)
dataTableFake = pd.DataFrame(dataZeros, index=tableDf.index, columns=tableDf.columns)
dataTable = tableDf.values.astype(str)
col = ["#dbdada"]
my_cmap = ListedColormap(col)
ax = sns.heatmap(dataTableFake, cmap=my_cmap, annot_kws={"size": 8}, cbar=False, linewidths=0.5,
square=False, ax=ax, annot = dataTable, fmt = '')
ax.xaxis.tick_top()
ax.set_yticklabels(ax.get_yticklabels(), rotation=0)
table = pd.DataFrame(dict(value01=1.01,
value02=2.02,
value03=3.03),
index=["This is a pretty long index and I dont't want it to shrink the other subplots"])
gs = gridspec.GridSpec(2, 1)
ax1 = plt.subplot(gs[0, 0])
ax2 = plt.subplot(gs[1, 0])
plotTable(tableDf=table, ax=ax1)
plt.tight_layout()
创建以下图:
我不希望第二个轴(即 ax2)受到 ax1 中行名长度的影响。我希望这能说明我的意思:
【问题讨论】:
标签: python pandas matplotlib seaborn