Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 212
nom-mon-ir Avatar asked Mar 04 '13 20:03

nom-mon-ir


People also ask

How do I add a label to a plot in pandas?

You can set the labels on that object. Or, more succinctly: ax. set(xlabel="x label", ylabel="y label") . Alternatively, the index x-axis label is automatically set to the Index name, if it has one.

How do you graph a vertical line in a time series?

To create a time series plot, we can use simply apply plot function on time series object and if we want to create a vertical line on that plot then abline function will be used with v argument.


1 Answers

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.

E.g.

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()

enter image description here

like image 148
Joe Kington Avatar answered Oct 21 '22 21:10

Joe Kington