PL03-Topic02, Matplotlib, Annotation
Back to the previous page
List of posts to read before reading this article
Contents
Basic annotation
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5,10, 1000)
y = np.sin(x-np.pi/2)
fig, ax = plt.subplots()
ax.plot(x,y)
ax.annotate('local max',
xy=(3, 1), xycoords='data',
xytext=(0.8, 0.95), textcoords='axes fraction',
arrowprops=dict(facecolor='black', shrink=0.05),
horizontalalignment='right',
verticalalignment='top')
Advanced Annotation
Annotating with Text with Box
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5,10, 1000)
y = np.sin(x-np.pi/2)
fig, ax = plt.subplots()
ax.plot(x,y)
ax.text(0, 0, "Direction",
ha="center",
va="center",
rotation=45,
size=15,
bbox={'boxstyle':"rarrow,pad=.1",
'fc':"cyan",
'ec':"b",
'lw':2})
SUPPLEMENT : boxstyle
Annotating with Arrow
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.annotate("",
xy=(0.2, 0.2), xycoords='data',
xytext=(0.8, 0.8), textcoords='data',
arrowprops=dict(arrowstyle="->",
connectionstyle="arc3"))
SUPPLEMENT : connectionstyle
SUPPLEMENT : arrowstyle
Placing Artist at the anchored location of the Axes
import matplotlib.pyplot as plt
from matplotlib import offsetbox
fig, ax = plt.subplots(1,1)
textbox = offsetbox.AnchoredText("hello\nhello", loc=1) # loc : auto-align
ax.add_artist(textbox)
Using Complex Coordinates with Annotations
Using ConnectionPatch
Advanced Topics
Zoom effect between Axes
Define Custom BoxStyle
List of posts followed by this article
Reference