Python Matplotlib Legend

preview_player
Показать описание

^Downloadable code & more! This video plots a lambda function and a list on the same plot using matplotlib pyplot as plt in python. This video shows you how to make a key or legend for the graph/plot. For more information on matplotlib watch some other videos I have made.

This is a Python anaconda tutorial for help with coding, programming, or computer science. These are short python videos dedicated to troubleshooting python problems and learning Python syntax. For more videos see Python Help playlist by Rylan Fowers.





MATPLOTLIB LEGENDS

From matplotlib we import pyplot as plt
and numpy as np

For this example we will make a line and some data points and plot them together with a legend

So let’s make a domain, we will do a numpy linespace from 0 to 10 with 50 points

and let’s make a lambda function for our line 2x - 1

and now we will make a list with some data pints

data 1 will be our x values

and data2 we will create some y values that the line can estimate

now we will plot one at at time, first with the lambda function let’s make it a blue dashed line and we will include the keyword label and call it line, this will be important for our legend

Similarly we will plot the data points and let’s make those magenta diamond dots. And we will put the keyword label again, so we can use that in our legend. we will type “Points”

Now let’s just add a title and some axis labels really quick. ( you can see my matplotlib plotting video for more details on that)

Here is the plot. With the blue dashed line and the magenta diamond points. We have our labels of the x and y axis. and the Title we put. And there is our beautiful legend that we created. It automatically will put it in the most visible spot.

There you have it, that is how you make a legend for your python matplolib plots

#PythonMarathon #LearnPython #PythonTutorial
Рекомендации по теме