This may be useful if you have many plots and want to be able to easily scan the code but not display the names in the plots. In this plot, the color of the lines has not changed but the order in the legend has changed because I changed the lists handles and labels.įinally, one thing many Python programmers don’t know is that if your label starts with an underscore, it will not be displayed in the legend. So, to make Square Root appear as the first entry in the legend, do the following. This is in contract to method 1 where the order you plot your lines is the order they appear in the legend. This means you can plot them in any order you want and still control the order of the legend entries. The biggest advantage of this method is that you have total control of the order in which the legend’s items appear: the order you pass handles is the order they will appear. You can skip this intermediate step if you wish. # linear_correct is a Line2D object - what you wantįor ease of reading, I created the lists handles and labels which I then passed to plt.legend(). The value is a 2D object which is how matplotlib stores lines. The function plt.plot() returns a list of length 1, so you must unpack it by putting a comma after your variable name to get the value inside the list. # Pass handles and lables to plt.legend()įirst, you must save the output of each line in a variable. # Save plots with descriptive variable names The handles are the lines you wish to appear on the legend and the labels are, as I hope you know by now, the word(s) you want to appear in the legend next to each line. The final method – plt.legend(handles, labels) – provides you with the most flexibility but takes slightly longer to write.īoth handles and labels are iterables – usually lists or tuples. Thus, both the matplotlib docs and I do not recommend you use this method. It is much easier for everyone if you explicitly label each of the plots rather than implicitly doing so like this. Lastly, it violates The Zen of Python Explicit is better than implicit because you implicitly label each plot based on its order. Moreover, if you change the order of any of your plots, you must also change the order of the elements in labels. For example, the iterable labels must be exactly the same length as the number of lines you draw. This method does work but can cause you a lot of headaches. You do not label any of the lines explicitly and instead label them based on the order they appear: plt.plot(x_1) Instead of explicitly labeling each line you draw like so: plt.plot(x_1, label='plot_1') The argument labels must be an iterable – most likely a list or tuple – containing the labels you want to display in the legend. The second option – plt.legend(labels) – is rarely used but I’ll show you what it does just for completeness. Note: you can manually control the colors using the c keyword argument if you want to. By changing the order of the plots, you change not only the order in the legend but also the colors of the lines. Now vals/2 is displayed first in the legend and is colored blue. You can see that vals is displayed first in the legend and is a blue line. vals = np.array()įirst I plotted vals and then plotted vals/2. The order of the lines in the legend are the same as the order you plot them. It does this by displaying all plots that have been labeled with the label keyword argument. The first option – plt.legend() – automatically detects which elements to show. To display a legend on any plot, you must call plt.legend() at some point in your code – usually, just before plt.show() is a good place. Let’s dive into a more detailed example of how legends work in matplotlib. To learn more about Python’s random module, check out my article. So, I manually changed it to red with the c keyword argument. Unfortunately, matplotlib does not automatically change the color of each plot if you plot a line and a scatter plot on top of each other. Then, I drew a line plot on top of it to act as the line of best fit (note that this is just an example and isn’t actually the line of best fit for this dataset!). In this example, I first generated some random data before making a scatter plot from it. # Red line plot acting as the 'line of best fit' – but you may have to specify the colors manually if you do. You can combine different types of plot – scatter, line, histogram etc. If you plot and label multiple lines, the legend will contain multiple entries. # Optional: Use seaborn style as it looks nicer than matplotlib's default Prettier Example # Import necessary modules
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |