Here we discuss an introduction to Matplotlib Legend, along with examples in detail explanation for better understanding. The ‘Legend’ method in matplotlib is used to create labels for the plots, which help us in differentiating the functions drawn in the plot. They help us in understanding any relation between the variables and also to have a high-level understanding of data without actually viewing it. Plots become very handy when we are trying to understand the data intuitively. Z.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), ncol = 2)Įxplanation: As we can see in our output, the label box is now outside the plot and with 2 columns. Passing these pre-defined codes as an argument will help us to create the label box outside our plot Note that In next line of code, we have added a couple of new arguments. Import matplotlib.pyplot as pltĪ = ī = Ĭ = We will create a new plot of 2 straight lines to understand this.
Matlab plot legend how to#
Next we will understand how to set our label box outside the plot. We will create a new plot of 2 straight lines to understand this: So, we can get the direction as per our requirement, by changing the value of ‘loc’. Note that our label box is now in the bottom left. This is how our output will look like in python: You also can create a legend with multiple columns or create a legend for a subset of the plotted data. These examples show how to create a legend and make some common modifications, such as changing the location, setting the font size, and adding a title. Our code will essentially be the same as above, with only change that loc will now be 3: Legends are a useful way to label data series plotted on a graph. Let us change the value of loc to 3 and see how our output changes. We can set ‘loc’ to different integer values to change the direction of the label box within the plot. This direction is obtained because we have passed the argument ‘loc’ = 1 in our legend method. Note: The label box on the top right side. For this example, we will use sine and cosine functions Next, let us define our functions for the plot. Hadoop, Data Science, Statistics & others Example #1