HomeLinuxSeaborn Legend

Seaborn Legend


Information visualization is important in conveying data successfully. When creating visible representations of information utilizing “Seaborn”, one important ingredient to contemplate is the “legend”. The “legend” is a visible information that explains the which means of various components in a plot. It supplies key details about the plotted knowledge, thereby serving to viewers to know the relationships and patterns inside the visualization.

This text explores a complete information on the “Seaborn Legend” utilizing quite a few examples.

Add a Fundamental Legend in Seaborn?

A “legend” is robotically added to seaborn graphs by default and so as to add/create a legend to a “seaborn” plot, the “legend()” operate of the “matplotlib” library is utilized in Python. This operate takes a number of parameters akin to “loc”, “title”, “labels” and “handles”.

Instance

The next code is used so as to add/insert a legend to the plot:

import seaborn
import matplotlib.pyplot as plt
knowledge = seaborn.load_dataset(‘ideas’)
seaborn.scatterplot(knowledge=knowledge, x=‘total_bill’, y=‘tip’, hue=‘day’)
plt.legend(loc=‘higher left’, title=‘Day of Week’, fontsize=12)
plt.present()

 

Within the above code:

  • The “seaborn” and “matplotlib” libraries are imported initially of this system, respectively.
  • The “load_dataset()” technique is used to load the pre-defined dataset “ideas” and assign it to a variable named “knowledge”.
  • The “scatterplot()” technique is used to plot the scatter plot by taking the dataset and different parameters.
  • The matplotlib operate “legend()” is used so as to add the legend with the acknowledged title and font dimension to the higher left nook of the graph.

Output

The legend has been added to the seaborn plot appropriately.

Customise the Seaborn Legend?

To customise the seaborn “legend”, varied parameters of the “legend()” operate are utilized in Python.

Instance: Altering the Location of a Seaborn Legend

The legend “location” might be modified/up to date by utilizing the “loc” parameter of the “legend()” operate. This parameter/argument is usually a “string/str” or an “integer/int”. For an in depth understanding, you may examine this particular information. The beneath instance code is utilized to replace the “location” of the seaborn “legend”:

import seaborn
import matplotlib.pyplot as plt
knowledge = seaborn.load_dataset(‘ideas’)
seaborn.scatterplot(knowledge=knowledge, x=‘total_bill’, y=‘tip’, hue=‘day’)
plt.legend(loc=‘higher middle’, title=‘Day of Week’, fontsize=12)
plt.present()

 

Within the above code, the assigned “loc= ‘higher middle’” parameter is used to vary the situation of the seaborn legend and place it on the higher middle of the plot.

Output

On this consequence, the situation of the legend has been modified efficiently utilizing the “loc” parameter.

Word: The dimensions, font, colour, and title of the legend will also be modified by utilizing the “fontsize”, “fontname”, “colour” and “title” parameters of the “legend()” operate, respectively.

Conclusion

A “legend” is robotically added to seaborn graphs by default and so as to add it manually, apply the “plt.legend()” operate in Python. To customise a legend based mostly on its location, the “dimension”, “font”, “colour”, “title”, and many others. parameters of the “plt.legend()” operate can be utilized. This information offered varied methods so as to add a legend to the seaborn plot.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments