# In[1]
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib as mpl
plt.style.use('seaborn-whitegrid')
import numpy as np
plt.text
/ax.text
functions, which will place text at a particular value.ax.text
method takes an position, a position, a string, and then optional keywords specifying the color, size, alignment, and other properties of the text.ha
is short for horizontal alignmentFor more information on the available options, refer these url :
1. plt.text documentation
2. mpl.text.Text documentation
matplotlib.transforms
submodule)ax.transData
: Transform associated with data coordinates
ax.transAxes
: Transform associated with the axes (in units of axes dimensions)
fig.transFigure
: Transform associated with the figure (in units of figure dimensions)
# In[2]
fig,ax=plt.subplots(facecolor='lightgray')
ax.axis([0,10,0,10])
# transform=ax.transData is the default, but we'll specify it anyway
ax.text(1,5,". Data: (1,5)",transform=ax.transData)
ax.text(0.5,0.1,". Axes: (0.5,0.1)",transform=ax.transAxes)
ax.text(0.2,0.2,". Figure: (0.2,0.2)",transform=fig.transFigure);
transData
coordinates give the usual data coordinates associated with the x- and y-axis labels.transAxes
coordinates give the location from the bottom-left corner of the axes, as a fraction of the total axes size.transFigure
coordinates are similar, but specify the position from the bottom-left corner of the figure as a fraction of the total figure size.transData
coordinates that will be affected, while the others remain stationary.# In[3]
ax.set_xlim(0,2)
ax.set_ylim(-6,6)
fig
%matplotlib inline
to %matplotlib notebook
and using each plot's menu to interact with the plot.plt.arrow
fuction available, I wouldn't suggest using it: the arrows it creates are SVG objects that will be subject to the varying aspect ratio of your plots, making it tricky to get them right.plt.annotate
fucntion, which creates some text and an arrow an allows the arrows to be very flexibly specified.# In[4]
fig,ax=plt.subplots()
x=np.linspace(0,20,1000)
ax.plot(x,np.cos(x))
ax.axis('equal')
ax.annotate('local maximum',xy=(6.28,1),xytext=(10,4),
arrowprops=dict(facecolor='black',shrink=0.05))
ax.annotate('local minimum',xy=(5 * np.pi, -1),xytext=(2,-6),
arrowprops=dict(arrowstyle='->',connectionstyle="angle3,angleA=0,angleB=-90"));
arrowprops
dictionary, which has numerous options available.