| 1 |
""" |
| 2 |
Module with useful functions for making publication-ready plots. |
| 3 |
""" |
| 4 |
|
| 5 |
|
| 6 |
def adjust_spines(ax, spines, position=5): |
| 7 |
""" |
| 8 |
Set custom visibility and position of axes |
| 9 |
|
| 10 |
ax : Axes |
| 11 |
Axes handle |
| 12 |
spines : List |
| 13 |
String list of 'left', 'bottom', 'right', 'top' spines to show |
| 14 |
position : Integer |
| 15 |
Number of points for position of axis |
| 16 |
""" |
| 17 |
for loc, spine in ax.spines.items(): |
| 18 |
if loc in spines: |
| 19 |
spine.set_position(('outward', position)) |
| 20 |
else: |
| 21 |
spine.set_color('none') # don't draw spine |
| 22 |
|
| 23 |
# turn off ticks where there is no spine |
| 24 |
if 'left' in spines: |
| 25 |
ax.yaxis.set_ticks_position('left') |
| 26 |
elif 'right' in spines: |
| 27 |
ax.yaxis.set_ticks_position('right') |
| 28 |
else: |
| 29 |
# no yaxis ticks |
| 30 |
ax.yaxis.set_ticks([]) |
| 31 |
ax.tick_params(axis='y', which='both', left='off', right='off') |
| 32 |
|
| 33 |
if 'bottom' in spines: |
| 34 |
ax.xaxis.set_ticks_position('bottom') |
| 35 |
elif 'top' in spines: |
| 36 |
ax.xaxis.set_ticks_position('top') |
| 37 |
else: |
| 38 |
# no xaxis ticks |
| 39 |
ax.xaxis.set_ticks([]) |
| 40 |
ax.tick_params(axis='x', which='both', bottom='off', top='off') |
| 41 |
|
| 42 |
|
| 43 |
def adjust_ylabels(ax,x_offset=0): |
| 44 |
''' |
| 45 |
Scan all ax list and identify the outmost y-axis position. |
| 46 |
Setting all the labels to that position + x_offset. |
| 47 |
''' |
| 48 |
|
| 49 |
xc = 0.0 |
| 50 |
for a in ax: |
| 51 |
xc = min(xc, (a.yaxis.get_label()).get_position()[0]) |
| 52 |
|
| 53 |
for a in ax: |
| 54 |
a.yaxis.set_label_coords(xc + x_offset, |
| 55 |
(a.yaxis.get_label()).get_position()[1]) |