Reputation: 1671
When I execute the following code, it doesn't produce a plot with a label.
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(1, 5)
plt.plot(x, x*1.5, label='Normal')
Numpy version is '1.6.2' Matplotlib version is '1.3.x'
Why is this happening?
Upvotes: 52
Views: 86301
Reputation: 1
to add label
plt.xlabel('insert your label')
plt.ylabel('insert your label')
plt.title('insert title')
Upvotes: 0
Reputation: 23131
If you want to show the labels next to the lines, there's a matplotlib extension package matplotx
(can be installed via pip install matplotx[all]
) that has a method that does that.
import matplotx
x = np.arange(1, 5)
plt.plot(x, x*1.5, label='Normal')
plt.plot(x, x*2, label='Quadratic')
matplotx.line_labels()
N.B. A similar task using only matplotlib would look like:
x = np.arange(1, 5)
y = x*1.5
plt.plot(x, y)
plt.text(x[-1]*1.05, y[-1], 'Normal', color=plt.gca().get_lines()[0].get_color());
Upvotes: 2
Reputation: 169304
You forgot to display the legend:
...
plt.legend(loc='best')
plt.show()
Upvotes: 138