简体   繁体   English

matplotlib / pandas:在时间序列图中沿着绘制线放置线标签

[英]matplotlib/pandas: put line label along the plotted lines in time series plot

I am plotting time series data comparing system characteristics from multiple nodes. 我正在绘制比较多个节点的系统特征的时间序列数据。 I want to label the line from a particular node explicitly along its line. 我想明确地沿着它的线标记来自特定节点的线。 So far I have succeeded in putting separate line style for the particular node, which gives it distinctive line and distinctive style marker in the legend box. 到目前为止,我已经成功地为特定节点添加了单独的线条样式,这使其在图例框中具有独特的线条和独特的样式标记。

I am trying to find a way to put distinctive label along the line, possibly text curving along the line. 我试图找到一种方法,沿着线放置独特的标签,可能是沿着线弯曲的文本。 Any way to achieve that? 有什么方法可以实现吗?

Text curving along the line isn't easy to do with matplotlib, but annotate will allow you to easily label the line with text and an arrow. 沿着线条弯曲的文本对于matplotlib来说并不容易,但是annotate将允许您使用文本和箭头轻松标记线条。

Eg 例如

import matplotlib.pyplot as plt
import numpy as np

# Generate some data
x = np.linspace(0, 20, 200)
y = np.cos(x) + 0.5 * np.sin(3 * x)

fig, ax = plt.subplots()
ax.plot(x, y)

ax.annotate(r'$y = cos(x) + \frac{sin(3x)}{2}$', xy=(x[70], y[70]), 
            xytext=(20, 10), textcoords='offset points', va='center',
            arrowprops=dict(arrowstyle='->'))
plt.show()

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM