Skip to content

Instantly share code, notes, and snippets.

@zengyu714
Last active November 4, 2017 01:31
Show Gist options
  • Save zengyu714/8234b81a5902ef5f25dd14ca6021fc01 to your computer and use it in GitHub Desktop.
Save zengyu714/8234b81a5902ef5f25dd14ca6021fc01 to your computer and use it in GitHub Desktop.
plot-utils
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
# E.g., plot focal loss
def focal_loss(p, gamma):
return -np.power((1 - p), gamma) * np.log(p)
fig, ax = plt.subplots(figsize=[16, 5])
[ax.plot(xs, focal_loss(xs, g), label='$\gamma = {}$'.format(g)) for g in [0, 0.5, 1, 2, 5]]
ax.set_xlabel('')
ax.set_ylabel('')
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.legend()
# zoom-factor: (int), location: (int) center, bbox_to_anchor: (tuple) need adjustment
axins = zoomed_inset_axes(ax, zoom_factor, loc=10, bbox_to_anchor=(680, 200))
[axins.plot(xs, focal_loss(xs, g)) for g in [0, 0.5, 1, 2, 5]]
# specify the limits
x1, x2, y1, y2 = 0.7, 0.9, -0.05, 0.4
axins.set_xlim(x1, x2) # apply the x-limits
axins.set_ylim(y1, y2) # apply the y-limits
axins.set_title('Well Classification Samples')
axins.spines["top"].set_visible(False)
axins.spines["right"].set_visible(False)
axins.grid()
mark_inset(ax, axins, loc1=1, loc2=3, fc="none", ec="0.5")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment