Skip to content

Commit

Permalink
Merge pull request #407 from NCAR/box2_with_set_spine_visibility
Browse files Browse the repository at this point in the history
box_2 with Viz function set_tick_direction_spine_visibility
  • Loading branch information
pilotchute committed Dec 8, 2022
2 parents 1f115ab + f86904c commit a376961
Showing 1 changed file with 69 additions and 10 deletions.
79 changes: 69 additions & 10 deletions Gallery/Boxplots/NCL_box_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,67 @@
for a in range(len(data)):
data[a] = [x - 4 for x in data[a]]

###############################################################################
# Helper function that will be released in GeoCAT-Viz. Delete this block after release.


def set_tick_direction_spine_visibility(ax,
tick_direction='out',
top_spine_visible=True,
bottom_spine_visible=True,
left_spine_visible=True,
right_spine_visible=True):
"""Utility function to turn off axes spines and set tickmark orientations.
Note: This function should be called after calling add_major_minor_ticks()
Args:
ax (:class:`matplotlib.axes._subplots.AxesSubplot` or :class:`cartopy.mpl.geoaxes.GeoAxesSubplot`):
Current axes to the current figure
tick_direction (:class:`str`):
Set 'in' to put ticks inside the axes,
'out' to put ticks outside the axes,
'inout' to put ticks both in and out of the axes.
top_spine_visible (:class:`bool`):
Set False to turn off top spine of the axes.
bottom_spine_visible (:class:`bool`):
Set False to turn off bottom spine of the axes.
left_spine_visible (:class:`bool`):
Set False to turn off left spine of the axes.
right_spine_visible (:class:`bool`):
Set False to turn off right spine.
"""
ax.tick_params(direction=tick_direction, axis='both', which='both')
ax.spines['top'].set_visible(top_spine_visible)
ax.spines['bottom'].set_visible(bottom_spine_visible)
ax.spines['left'].set_visible(left_spine_visible)
ax.spines['right'].set_visible(right_spine_visible)

if top_spine_visible and bottom_spine_visible:
ax.xaxis.set_ticks_position('default')
elif bottom_spine_visible and not top_spine_visible:
ax.xaxis.set_ticks_position('bottom')
elif top_spine_visible and not bottom_spine_visible:
ax.xaxis.set_ticks_position('top')
else:
ax.xaxis.set_ticks_position('none')

if left_spine_visible and right_spine_visible:
ax.yaxis.set_ticks_position('default')
elif not right_spine_visible and left_spine_visible:
ax.yaxis.set_ticks_position('left')
elif not left_spine_visible and right_spine_visible:
ax.yaxis.set_ticks_position('right')
else:
ax.yaxis.set_ticks_position('none')


###############################################################################
# Helper function to set edge color of boxes

Expand Down Expand Up @@ -66,10 +127,6 @@ def setBoxColor(boxplot, colors):
# Set boxplot edge colors
setBoxColor(boxplots, ['blue', 'red', '#66FF00'])

# Remove axis lines on top and right sides
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)

# Use geocat.viz.util convenience function to set axes tick values
gv.set_axes_limits_and_ticks(ax, ylim=(-6.0, 8.5), yticks=[-3.0, 0.0, 3.0, 6.0])

Expand All @@ -82,17 +139,19 @@ def setBoxColor(boxplot, colors):
x_minor_per_major=1,
labelsize=16)

# Use geocat.viz.util convenience function to set spines visibility
set_tick_direction_spine_visibility(ax,
tick_direction='in',
top_spine_visible=False,
right_spine_visible=False)

# Use geocat.viz.util convenience function to add title to the plot axis.
gv.set_titles_and_labels(ax,
maintitle='Tailored Box Plot',
maintitlefontsize=22)

# Make both major and minor ticks point inwards towards the plot
ax.tick_params(direction="in", which='both', pad=9)

# Set ticks only at left and bottom sides of plot
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
# Set padding between ticks and tick labels
ax.tick_params(pad=9)

# Display Plot
plt.tight_layout()
Expand Down

0 comments on commit a376961

Please sign in to comment.