Data visualization < day 5 >

Expand yesterday's image

The previous legend loc parameter is set to upper left to add to the upper right corner
The loc parameter also includes:

'best'           
'upper right'  
'upper left'   
'lower left'   
'lower right'  
'right'        
'center left'  
'center right' 
'lower center' 
'upper center' 
'center'   

Interested readers can try it on their own

Here are some special notes

We want to add a note to two function curves at 2 π / 3
First, we draw a point on the corresponding function image position
Then, draw a vertical line to the horizontal axis, mark with dotted line, and finally, write the label

Modified code:

...

t = 2*np.pi/3
plt.plot([t,t],[0,np.cos(t)], color ='blue', linewidth=2.5, linestyle="--")
plt.scatter([t,],[np.cos(t),], 50, color ='blue')
plt.annotate(
    r'$\cos(\frac{2\pi}{3})=-\frac{1}{2}$',
    xy=(t, np.cos(t)), xycoords='data',
    xytext=(-90, -50), textcoords='offset points', fontsize=16,
    arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2")
)  

plt.plot([t,t],[0,np.sin(t)], color ='red', linewidth=2.5, linestyle="--")
plt.scatter([t,],[np.sin(t),], 50, color ='red')
plt.annotate(
    r'$\sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$',
    xy=(t, np.sin(t)), xycoords='data',
    xytext=(+10, +30), textcoords='offset points', fontsize=16,
    arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2")
)

...

The following explains the meaning of the code:

plt.plot([t,t],[0,np.cos(t)], color ='blue', linewidth=2.5, linestyle="--")
plt.scatter([t,],[np.cos(t),], 50, color ='blue')

From (t,np.cos(t)) plumb line to x-axis, set the color as blue, word weight as 2.5, dotted line
The marker point (t,np.cos(t)) is 50(s=50) in size and blue in color

plt.annotate(
    r'$\cos(\frac{2\pi}{3})=-\frac{1}{2}$',
    xy=(t, np.cos(t)), xycoords='data',
    xytext=(-90, -50), textcoords='offset points', fontsize=16,
    arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2")
)  

Where parameter xycoords = data '
It means selecting a location based on the value of the data
xytext=(-90, -50) and textbooks ='offset points'
For the description of annotation position and xy deviation value, arrowprops is some settings of arrow type in the figure

For xy deviation value, readers need to adjust it according to the actual situation

The final rendering is as follows:


Posted on Sat, 04 Apr 2020 02:53:12 -0700 by boofboof