oldoc63 / learningDS

Learning DS with Codecademy and Books
0 stars 0 forks source link

Legends #480

Open oldoc63 opened 1 year ago

oldoc63 commented 1 year ago

When we have multiple lines on a single graph we can label them by using the command plt.legend(). The legend method takes a list with the labels to display.

oldoc63 commented 1 year ago

plt.legend() can also take a keyword argument loc, which will position the legend on the figure.

These are the position values loc accepts:

Number Code | String -- | -- 0 | best 1 | upper right 2 | upper left 3 | lower left 4 | lower right 5 | right 6 | center left 7 | center right 8 | lower center 9 | upper center 10 | center

Note: If you decide not to set a value for loc, it will default to choosing the “best” location.

oldoc63 commented 1 year ago

Sometimes, it's easier to label each line as we create it. If we want, we can use the keyword label inside of plt.plot(). If we choose to do this, we don't pass any labels into plt.legend().

oldoc63 commented 1 year ago
  1. The lines plotted represent the temperatures over the past year in three locations: Hyrule (hyrule) Kakariko (kakariko) Gerudo Valley (gerudo)

  2. Create a list of strings containing "Hyrule", "Kakariko", and "Gerudo Valley", and store it in a variable called legend_labels.

  3. Create a legend for the graph by feeding in legend_labels into plt.legend().

  4. Set the legend to be at the lower center of the chart.