在Matplotlib中,创建一个图例(example_legend()
,下图)并不太难,但我认为将标签放在正在绘制的曲线上(如example_inline()
中,下图)会更好。这可能非常麻烦,因为我必须手动指定坐标,并且,如果我重新格式化绘图,我可能必须重新定位标签。有没有办法在Matplotlib中自动生成曲线上的标签?能够以与曲线角度相对应的角度调整文本方向的加分。
import numpy as np
import matplotlib.pyplot as plt
def example_legend():
plt.clf()
x = np.linspace(0, 1, 101)
y1 = np.sin(x * np.pi / 2)
y2 = np.cos(x * np.pi / 2)
plt.plot(x, y1, label='sin')
plt.plot(x, y2, label='cos')
plt.legend()
def example_inline():
plt.clf()
x = np.linspace(0, 1, 101)
y1 = np.sin(x * np.pi / 2)
y2 = np.cos(x * np.pi / 2)
plt.plot(x, y1, label='sin')
plt.plot(x, y2, label='cos')
plt.text(0.08, 0.2, 'sin')
plt.text(0.9, 0.2, 'cos')
https://stackoverflow.com/questions/16992038
复制相似问题