Vlad Oles
Vlad Oles

Reputation: 122

How to display axis label on both sides of the figure in maptplotlib?

I want X-axis to be exactly the same both at the bottom and top of my figure. While the ticklabels can be duplicated using ax.tick_params(labeltop=True), it seems to be no analogous command for the label of the axis itself, as ax.xaxis.set_label_position('top') relocates the label instead of duplicating it.

All the similar questions that I found (e.g. In matplotlib, how do you display an axis on both sides of the figure?) seem to be only concerned about the ticklabels, not the axis labels.

I tried using ax.twiny() but it adds a black frame around my plot. Is there a cleaner, minimalist way of duplicating the axis label to the other side of the figure to complement the duplicated ticklabels?

EDIT: minimal working example of the black frame added by ax.twiny():

import seaborn as sns
from matplotlib import pyplot as plt

ax = sns.heatmap([[0, 1], [1, 0]], cbar=None) # no outline around the heatmap
ax.twiny() # adds black frame

Upvotes: 1

Views: 1836

Answers (1)

BigBen
BigBen

Reputation: 49998

(Not a matplotlib expert by any means, but regarding your edit):

  • sns.despine(left=True, bottom=True) will remove the spines.
  • You should be able to replicate the xticks, xticklabels, xlim, and xlabel on the twin Axes like so:
ax = sns.heatmap([[0, 1], [1, 0]], cbar=None) # no outline around the heatmap
ax.set_xlabel('foo')

ax1 = ax.twiny() # adds black frame

ax1.set_xticks(ax.get_xticks())
ax1.set_xticklabels(ax.get_xticklabels())
ax1.set_xlabel(ax.get_xlabel())
ax1.set_xlim(ax.get_xlim())

sns.despine(left=True, bottom=True) # remove spines

enter image description here

Upvotes: 1

Related Questions