Expanding on the above question, I found a solution to a similar problem when looking to rotate the values for the ticks on the axes.
The problem I ran into was with using Heatmap through Seaborn, and FacetGrid. I could rotate the x-axis ticks, but not the y-axis ticks by using...
for ax in fg.axes.flat:
ax.set_xticklabels(ax.get_xticklabels(), rotation=45)
ax.set_yticklabels(ax.get_yticklabels(), rotation=45)
I found the yticks were blank and wound up being replaced with a null list. This occurred because the final data in the column wrap col_wrap
had no yticks because I was aligning them on the left, so the 0th and 2nd yticks were overwritten by the 1st and 3rd yticks in my 2x2 grid matrix. The xticks did not have this problem, as it was the reverse for the xtick values, 0th and 2nd xticks were null while 1st and 3rd had values. I discovered this feature by printing the g.get_yticklabels()
:
g = sns.heatmap(d, **kwargs)
print(g.get_yticklabels())
[Text(0, 0.5, '5'), Text(0, 1.5, '8'), Text(0, 2.5, '12'), Text(0, 3.5, '15'), Text(0, 4.5, '19'), Text(0, 5.5, '22'), Text(0, 6.5, '25'), Text(0, 7.5, '26'), Text(0, 8.5, '29'), Text(0, 9.5, '33'), Text(0, 10.5, '34'), Text(0, 11.5, '36'), Text(0, 12.5, '40'), Text(0, 13.5, '43'), Text(0, 14.5, '47')]
[]
[Text(0, 0.5, '1'), Text(0, 1.5, '5'), Text(0, 2.5, '8'), Text(0, 3.5, '12'), Text(0, 4.5, '15'), Text(0, 5.5, '19'), Text(0, 6.5, '22'), Text(0, 7.5, '25'), Text(0, 8.5, '26'), Text(0, 9.5, '29'), Text(0, 10.5, '33'), Text(0, 11.5, '34'), Text(0, 12.5, '36'), Text(0, 13.5, '40'), Text(0, 14.5, '43'), Text(0, 15.5, '47')]
[]
My work-around for getting a rotation in both was to only perform if a {x,y}tick
was present:
g = sns.heatmap(d, **kwargs)
if g.get_yticklabels():
g.set_yticklabels(g.get_yticklabels(), rotation=30)
if g.get_xticklabels():
g.set_xticklabels(g.get_xticklabels(), rotation=45)
This was performed in the loop function, and then mapped to the FacetGrid. This assumes the {x,y}ticks
are equivalent for all four plots.
def draw_heatmap(*args, **kwargs):
data = kwargs.pop('data')
d = data.pivot(index=args[1], columns=args[0], values=args[2])
# High-Low limit values
hl = args[3]
lwrbnd, uprbnd = data[args[2]].describe([hl,1-hl]).iloc[[4,6]].values
g = sns.heatmap(d, **kwargs, vmin=lwrbnd, vmax=uprbnd)
if g.get_yticklabels():
g.set_yticklabels(labels=g.get_yticklabels(), rotation=30)
if g.get_xticklabels():
g.set_xticklabels(g.get_xticklabels(), rotation=45)
fg = sns.FacetGrid(multidf.groupby(findx).mean().reset_index(),
col_wrap=col_wrp, col=ondx, height=5, sharey=True)
fg.map_dataframe(draw_heatmap, 'col', 'row', toggle, hl, square=True,
cmap="YlGnBu")