Python数据可视化——添加标签:text()函数
1 text()函数
plt.text(x, y, s, fontdict=None, withdash=False, **kwargs)
2 简单示例
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(-1,1,10)
y=np.random.randn(10)
plt.scatter(x,y, marker='o',c=x,cmap=plt.cm.Pastel1)
for i in range(len(x)):
plt.text(x[i], y[i], i+1, fontsize=12, color = "r", style = "italic", weight = "light",
verticalalignment='center', horizontalalignment='right', rotation=0)
3 格式化字符串,保留2位小数
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(-1,1,10)
y=np.random.randn(10)
plt.scatter(x,y, marker='o',c=x,cmap=plt.cm.Pastel1)
for i in range(len(x)):
plt.text(x[i], y[i], "%.2f"% x[i], fontsize=12, color = "r", style = "italic", weight = "light",verticalalignment='center', horizontalalignment='right', rotation=0)