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)

 

posted @ 2023-01-29 15:20  小平凡的记录  阅读(117)  评论(0编辑  收藏  举报  来源