Skip to content

Instantly share code, notes, and snippets.

@jinto
Created January 31, 2025 14:34
Show Gist options
  • Save jinto/6e279d2db772fe0d590d72ffcf40b9dd to your computer and use it in GitHub Desktop.
Save jinto/6e279d2db772fe0d590d72ffcf40b9dd to your computer and use it in GitHub Desktop.
기초수학 with 파이썬
def arrowed_spines(fig, ax, remove_ticks=False):
"""
좌표축 화살표를 그리기 위한 함수
https://stackoverflow.com/questions/33737736/matplotlib-axis-arrow-tip
"""
xmin, xmax = ax.get_xlim()
ymin, ymax = ax.get_ylim()
# removing the default axis on all sides:
for side in ['bottom','right','top','left']:
ax.spines[side].set_visible(False)
if remove_ticks == True:
# removing the axis ticks
plt.xticks([]) # labels
plt.yticks([])
ax.xaxis.set_ticks_position('none') # tick markers
ax.yaxis.set_ticks_position('none')
# get width and height of axes object to compute
# matching arrowhead length and width
dps = fig.dpi_scale_trans.inverted()
bbox = ax.get_window_extent().transformed(dps)
width, height = bbox.width, bbox.height
# manual arrowhead width and length
hw = 1./50.*(ymax-ymin)
hl = 1./25.*(xmax-xmin)
lw = 1. # axis line width
ohg = 0.4 # arrow overhang
# compute matching arrowhead length and width
yhw = hw/(ymax-ymin)*(xmax-xmin)* height/width
yhl = hl/(xmax-xmin)*(ymax-ymin)* width/height
# draw x and y axis
ax.arrow(xmin, 0, xmax-xmin, 0., fc='k', ec='k', lw = lw,
head_width=hw, head_length=hl, #overhang = ohg,
length_includes_head= True, clip_on = False)
ax.arrow(0, ymin, 0., ymax-ymin, fc='k', ec='k', lw = lw,
head_width=yhw, head_length=yhl, #overhang = ohg,
length_includes_head= True, clip_on = False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment