course content

Course Content

Visualization in Python with matplotlib

Adding Legend to a PlotAdding Legend to a Plot

So far so good! You may remember that blue line refers to the first call of .plot() function, and the oragne line refers to the second call. But if someone other than you look on the chart, he will understand nothing. In this chapter, we will consider how to add a legend to a plot.

The easiest way to create a legend is to 'set' names while initializing plot. This can be done by setting the label parameter of .plot() function. After the labels are set, we can call plt.legend() before plt.show() to display the legend on the plot. For example, the code from the previous example can be modified in the following way:

Everything was clear?

Section 1. Chapter 6