# coding: utf-8
import matplotlib.pyplot as plt
import numpy as np
x1 = np.random.normal(30, 3, 100)
x2 = np.random.normal(20, 2, 100)
x3 = np.random.normal(10, 3, 100)
# 隨機正態分布生成x1、x2、x3,並為每個plot指定一個字符串標簽
plt.plot(x1, label="plot")
plt.plot(x2, label="plot2")
plt.plot(x3, label="plot3")
# 設置loc參數確定圖例框的位置
# 設置列數為nloc=3,指定邊框的(bbox_to_anchor)的起始位置為(0.0,1.02),設置寬度為1,高度為0.102,注意這些值都是基於歸一化坐標系。參數mode可以設置為None或者expand,當為expand時,圖例框會水平的擴展至整個坐標軸區域。參數borderaxespad指定了坐標軸和圖例邊界之間的間距。
plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
ncol=3, mode="expand", borderaxespad=0.)
# 進行備注
plt.annotate("Important value", (55,20), xycoords='data',
xytext=(5, 38),
arrowprops=dict(arrowstyle='->'))
plt.show()

plt.annotate()函數解析:
# plt.annotate()函數用於標注文字
plt.annotate(s='str', xy=(x,y) , xytext=(l1,l2) , ... )
參數解釋:
s 為注釋文本內容
xy 為被注釋的坐標點
xytext 為注釋文字的坐標位置
xycoords 參數如下:
figure points:圖左下角的點
figure pixels:圖左下角的像素
figure fraction:圖的左下部分
axes points:坐標軸左下角的點
axes pixels:坐標軸左下角的像素
axes fraction:左下軸的分數
data:使用被注釋對象的坐標系統(默認)
polar(theta,r):if not native ‘data’ coordinates t
weight 設置字體線型
{‘ultralight’, ‘light’, ‘normal’, ‘regular’, ‘book’, ‘medium’, ‘roman’, ‘semibold’, ‘demibold’, ‘demi’, ‘bold’, ‘heavy’, ‘extra bold’, ‘black’}
color 設置字體顏色
{‘b’, ‘g’, ‘r’, ‘c’, ‘m’, ‘y’, ‘k’, ‘w’}
‘black’,'red’等
[0,1]之間的浮點型數據
RGB或者RGBA, 如: (0.1, 0.2, 0.5)、(0.1, 0.2, 0.5, 0.3)等
arrowprops #箭頭參數,參數類型為字典dict
width:箭頭的寬度(以點為單位)
headwidth:箭頭底部以點為單位的寬度
headlength:箭頭的長度(以點為單位)
shrink:總長度的一部分,從兩端“收縮”
facecolor:箭頭顏色
bbox給標題增加外框 ,常用參數如下:
boxstyle:方框外形
facecolor:(簡寫fc)背景顏色
edgecolor:(簡寫ec)邊框線條顏色
edgewidth:邊框線條大小
# coding: utf-8
import matplotlib.pyplot as plt
import numpy as np
def test1():
x = np.arange(0, 50, 5)
print(x)
y = x * x
plt.plot(x, y, marker="o")
for xy in zip(x,y):
plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(2, 4), textcoords='offset points')
plt.show()
# test1()
def test2():
x = np.arange(0,50, 5)
y = x * x
plt.plot(x, y, marker='o')
# weight:增加字體線型
for xy in zip(x, y):
plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-20, 10), textcoords='offset points', weight='heavy')
plt.show()
# test2()
def test3():
x = np.arange(0,50,5)
y = x * x
plt.plot(x, y, marker='o')
# 增加字體的顏色
for xy in zip(x, y):
plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-20, 10), textcoords='offset points', color='y', weight='heavy')
plt.show()
# test3()
def test4():
x = np.arange(0, 50, 10)
y = x * x
plt.plot(x, y, marker='o')
# 增加箭頭
for xy in zip(x, y):
plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-20, 10), textcoords='offset points', color='y', weight='heavy',
arrowprops=dict(facecolor="y", headlength=10, headwidth=10, width=20))
plt.show()
# test4()
def test5():
x = np.arange(0, 50, 10)
y = x * x
plt.plot(x, y, marker='o')
# 增加標題外框
for xy in zip(x, y):
plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-20, 10), textcoords='offset points',color='y', weight='heavy',
arrowprops=dict(facecolor="y", headlength=10, headwidth=10, width=20),
bbox=dict(boxstyle='round,pad=0.5', fc='yellow', ec='k', lw=1, alpha=0.5))
plt.show()
# test5()
def test6():
x = np.arange(0, 50, 10)
y = x * x
plt.plot(x, y, marker='o')
# 增加箭頭指示
for xy in zip(x, y):
# plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-20, 10), textcoords='offset points', color='y', weight='heavy',
# arrowprops=dict(facecolor="y", headlength=10, headwidth=10, width=20),
# bbox=dict(boxstyle='round,pad=0.5', fc='yellow', ec='k', lw=1, alpha=0.5))
plt.annotate('local max', xy=(5, 25), xytext=(20, 25), arrowprops=dict(facecolor='red', shrink=0.05))
plt.show()
# test6()