如何更改某些轴上的 seaborn 热图 tick_params 文本方向并使其完全适合图片
How to change seaborn heatmap tick_params text orientation on some axes and make it fit in the picture completely
我有一个 seaborn 热图,如下所示:
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
results_changed = [['equal','equal','smaller','smaller or equal','greater or equal'],
['equal','equal','smaller','smaller','greater or equal'],
['greater or equal','equal','smaller or equal','smaller','smaller'],
['equal','smaller or equal','greater or equal','greater or equal','equal'],
['equal','equal','smaller','equal','equal']]
index = ['axc123abc', 'org567def', 'cf5010wer', 'cm1235ert', 'ext447tyu']
columns = ['axc123abc', 'org567def', 'cf5010wer', 'cm1235ert', 'ext447tyu']
# create a dataframe
res_df = pd.DataFrame(results_changed, columns, index)
#construct dictionary from ordered list
category_order = ['greater', 'greater or equal', 'equal', 'smaller or equal', 'smaller']
value_to_int = {j:i for i,j in enumerate(category_order)}
n = len(value_to_int)
# discrete colormap (n samples from a given cmap)
cmap = sns.color_palette("flare", n)
ax = sns.heatmap(res_df.replace(value_to_int), cmap=cmap, vmin=0, vmax=n)
#modify colorbar:
colorbar = ax.collections[0].colorbar
colorbar.set_ticks([0.5 + i for i in range(n)])
colorbar.set_ticklabels(category_order)
ax.tick_params(right=True, top=True, labelright=True, labeltop=True)
plt.tight_layout()
plt.show()
我想通过从图的每一侧(不仅仅是底部和左侧)为轴添加刻度描述来使其更具可读性。我已经设法通过添加一行代码来做到这一点,如下所示:
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
results_changed = [['equal','equal','smaller','smaller or equal','greater or equal'],
['equal','equal','smaller','smaller','greater or equal'],
['greater or equal','equal','smaller or equal','smaller','smaller'],
['equal','smaller or equal','greater or equal','greater or equal','equal'],
['equal','equal','smaller','equal','equal']]
index = ['axc123abc', 'org567def', 'cf5010wer', 'cm1235ert', 'ext447tyu']
columns = ['axc123abc', 'org567def', 'cf5010wer', 'cm1235ert', 'ext447tyu']
# create a dataframe
res_df = pd.DataFrame(results_changed, columns, index)
#construct dictionary from ordered list
category_order = ['greater', 'greater or equal', 'equal', 'smaller or equal', 'smaller']
value_to_int = {j:i for i,j in enumerate(category_order)}
n = len(value_to_int)
# discrete colormap (n samples from a given cmap)
cmap = sns.color_palette("flare", n)
ax = sns.heatmap(res_df.replace(value_to_int), cmap=cmap, vmin=0, vmax=n)
#modify colorbar:
colorbar = ax.collections[0].colorbar
colorbar.set_ticks([0.5 + i for i in range(n)])
colorbar.set_ticklabels(category_order)
# newly added code line
ax.tick_params(right=True, top=True, labelright=True, labeltop=True)
plt.tight_layout()
plt.show()
问题是,这些添加的刻度描述并不完全可见(由于它们的长度)。我不确定如何旋转特定轴上的刻度描述。我希望上部的刻度线垂直放置(就像底部的刻度线一样),右侧的刻度线水平放置(就像左边的刻度线一样)。我怎样才能做到这一点并在图中很好地拟合热图、轴的刻度描述和颜色条?如果有任何建议,我将不胜感激。
你可以这样做:
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
results_changed = [['equal','equal','smaller','smaller or equal','greater or equal'],
['equal','equal','smaller','smaller','greater or equal'],
['greater or equal','equal','smaller or equal','smaller','smaller'],
['equal','smaller or equal','greater or equal','greater or equal','equal'],
['equal','equal','smaller','equal','equal']]
index = ['axc123abc', 'org567def', 'cf5010wer', 'cm1235ert', 'ext447tyu']
columns = ['axc123abc', 'org567def', 'cf5010wer', 'cm1235ert', 'ext447tyu']
# create a dataframe
res_df = pd.DataFrame(results_changed, columns, index)
# construct dictionary from ordered list
category_order = ['greater', 'greater or equal', 'equal', 'smaller or equal', 'smaller']
value_to_int = {j: i for i,j in enumerate(category_order)}
n = len(value_to_int)
# discrete colormap (n samples from a given cmap)
cmap = sns.color_palette("flare", n)
ax = sns.heatmap(res_df.replace(value_to_int), cmap=cmap, vmin=0, vmax=n, cbar_kws=dict(pad=0.2))
# modify colorbar:
colorbar = ax.collections[0].colorbar
colorbar.set_ticks([0.5 + i for i in range(n)])
colorbar.set_ticklabels(category_order)
# newly added code line
ax.tick_params(right=True, top=True, labelright=True, labeltop=True)
plt.xticks(rotation=90)
plt.yticks(rotation=0)
plt.tight_layout()
plt.show()
输出:
我有一个 seaborn 热图,如下所示:
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
results_changed = [['equal','equal','smaller','smaller or equal','greater or equal'],
['equal','equal','smaller','smaller','greater or equal'],
['greater or equal','equal','smaller or equal','smaller','smaller'],
['equal','smaller or equal','greater or equal','greater or equal','equal'],
['equal','equal','smaller','equal','equal']]
index = ['axc123abc', 'org567def', 'cf5010wer', 'cm1235ert', 'ext447tyu']
columns = ['axc123abc', 'org567def', 'cf5010wer', 'cm1235ert', 'ext447tyu']
# create a dataframe
res_df = pd.DataFrame(results_changed, columns, index)
#construct dictionary from ordered list
category_order = ['greater', 'greater or equal', 'equal', 'smaller or equal', 'smaller']
value_to_int = {j:i for i,j in enumerate(category_order)}
n = len(value_to_int)
# discrete colormap (n samples from a given cmap)
cmap = sns.color_palette("flare", n)
ax = sns.heatmap(res_df.replace(value_to_int), cmap=cmap, vmin=0, vmax=n)
#modify colorbar:
colorbar = ax.collections[0].colorbar
colorbar.set_ticks([0.5 + i for i in range(n)])
colorbar.set_ticklabels(category_order)
ax.tick_params(right=True, top=True, labelright=True, labeltop=True)
plt.tight_layout()
plt.show()
我想通过从图的每一侧(不仅仅是底部和左侧)为轴添加刻度描述来使其更具可读性。我已经设法通过添加一行代码来做到这一点,如下所示:
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
results_changed = [['equal','equal','smaller','smaller or equal','greater or equal'],
['equal','equal','smaller','smaller','greater or equal'],
['greater or equal','equal','smaller or equal','smaller','smaller'],
['equal','smaller or equal','greater or equal','greater or equal','equal'],
['equal','equal','smaller','equal','equal']]
index = ['axc123abc', 'org567def', 'cf5010wer', 'cm1235ert', 'ext447tyu']
columns = ['axc123abc', 'org567def', 'cf5010wer', 'cm1235ert', 'ext447tyu']
# create a dataframe
res_df = pd.DataFrame(results_changed, columns, index)
#construct dictionary from ordered list
category_order = ['greater', 'greater or equal', 'equal', 'smaller or equal', 'smaller']
value_to_int = {j:i for i,j in enumerate(category_order)}
n = len(value_to_int)
# discrete colormap (n samples from a given cmap)
cmap = sns.color_palette("flare", n)
ax = sns.heatmap(res_df.replace(value_to_int), cmap=cmap, vmin=0, vmax=n)
#modify colorbar:
colorbar = ax.collections[0].colorbar
colorbar.set_ticks([0.5 + i for i in range(n)])
colorbar.set_ticklabels(category_order)
# newly added code line
ax.tick_params(right=True, top=True, labelright=True, labeltop=True)
plt.tight_layout()
plt.show()
问题是,这些添加的刻度描述并不完全可见(由于它们的长度)。我不确定如何旋转特定轴上的刻度描述。我希望上部的刻度线垂直放置(就像底部的刻度线一样),右侧的刻度线水平放置(就像左边的刻度线一样)。我怎样才能做到这一点并在图中很好地拟合热图、轴的刻度描述和颜色条?如果有任何建议,我将不胜感激。
你可以这样做:
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
results_changed = [['equal','equal','smaller','smaller or equal','greater or equal'],
['equal','equal','smaller','smaller','greater or equal'],
['greater or equal','equal','smaller or equal','smaller','smaller'],
['equal','smaller or equal','greater or equal','greater or equal','equal'],
['equal','equal','smaller','equal','equal']]
index = ['axc123abc', 'org567def', 'cf5010wer', 'cm1235ert', 'ext447tyu']
columns = ['axc123abc', 'org567def', 'cf5010wer', 'cm1235ert', 'ext447tyu']
# create a dataframe
res_df = pd.DataFrame(results_changed, columns, index)
# construct dictionary from ordered list
category_order = ['greater', 'greater or equal', 'equal', 'smaller or equal', 'smaller']
value_to_int = {j: i for i,j in enumerate(category_order)}
n = len(value_to_int)
# discrete colormap (n samples from a given cmap)
cmap = sns.color_palette("flare", n)
ax = sns.heatmap(res_df.replace(value_to_int), cmap=cmap, vmin=0, vmax=n, cbar_kws=dict(pad=0.2))
# modify colorbar:
colorbar = ax.collections[0].colorbar
colorbar.set_ticks([0.5 + i for i in range(n)])
colorbar.set_ticklabels(category_order)
# newly added code line
ax.tick_params(right=True, top=True, labelright=True, labeltop=True)
plt.xticks(rotation=90)
plt.yticks(rotation=0)
plt.tight_layout()
plt.show()
输出: