Efficiently Cache And Restore Matplotlib Axes Parameters After Moving Spines
My Problem I'm having trouble maintaining formatting and modifications applied to a matplotlib Axes object after offsetting the spines. An example Consider the following simplifie
Solution 1:
I modified you code, and it can product the same ticks now.
%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib.artist import ArtistInspector
def funky_formatting(ax):
ax.set_xticks([0.1, 0.2, 0.5, 0.7, 0.9])
ax.set_xticklabels(list('abcde'), rotation=60)
ax.set_yticks([0.2, 0.5, 0.7, 0.8])
ax.set_yticklabels(list('ABCD'), rotation=35)
ax.tick_params(axis='both', labelsize=18, labelcolor='r')
ax.set_ylabel('r$y_{\mathrm{ii}}$ test', color='b', fontweight='extra bold', fontsize=20)
ax.set_xlabel('r$y_{\mathrm{ii}}$ test', color='r', fontweight='light', fontsize=16)
def try_update(artist, p):
for k,v in p.iteritems():
try:
artist.update({k:v})
except:
pass
def offset_spines(ax):
for spine in ax.spines.values():
paxis = spine.axis.properties()
ptick = [label.properties() for label in spine.axis.get_ticklabels()]
spine.set_position(('outward', 10))
try_update(spine.axis, paxis)
for label, p in zip(spine.axis.get_ticklabels(), ptick):
p.pop("transform")
try_update(label, p)
# create two axes
fig, axes = plt.subplots(nrows=2)
# format both axes the same way:
for ax in axes:
funky_formatting(ax)
# offset the spines of only the top subplot
offset_spines(axes[0])
fig.tight_layout()
Here is the output:
Post a Comment for "Efficiently Cache And Restore Matplotlib Axes Parameters After Moving Spines"