Label in matplotlib doesnot work

Dear all,
could anybody explain for me why label = ‘all devs’ and label =‘python’ does not work in matplotlib

thanks alot

from matplotlib import pyplot as plt

dev_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
dev_y = [2000, 2300, 2400, 2500, 2700, 2800, 2900, 3000, 3100, 3200, 3300]

plt.plot(dev_x, dev_y, label=“All Devs”)
py_dev_y = [3000, 3200, 3500, 4000, 4100, 4200, 4300, 4500, 4700, 4900, 5000]

plt.plot(dev_x, py_dev_y, label=“Python”)

plt.title(‘Median salary (USD) by Age’)
plt.xlabel(‘Ages’)
plt.ylabel(‘Median Salary (USD)’)
plt.show()

AFAIK, pyplot.plot has no label parameter. Take a look at the matplotlib.pyplot docs for examples of how to use labels.

Also, take a look at the About the users category for how to correctly format your code in future posts.

you missed plt.legend() here. Add it and the legend will show up.

1 Like

Diksha is right: a call to legend() is required to display the labels. (The matplotlib documentation will almost everywhere show it if it had to be used as a method of an “axis” (ax.legend()), but it can be imported and used as an independent function (and then collect/process all labels in all “open” plots).

Erlend is wrong, plot(…, label=“…”) is correct to add a label to the graphs, very useful if you have multiple graphs (“lines”) in one plot, or even if you have only one graph in the plot, it’s useful to add a kind of “inline title”: The legend will show the style of the curve (e.g. a small piece of an orange line) and next to it the given label.

The bigger question is this sample data or real data? :grinning: