Move seaborn plot legend to a different position in Python
In this tutorial we will learn how to move the seaborn plot legend to a different position in python.
In Python, when visualizing using Seaborn, the legend is very important to show what the plot conveys. It is possible that you might want to change the default position of a legend if it does not seem convenient for your plot. This blog post provides an insight on how to relocate a legend in Seaborn.
Seaborn is a powerful and high-level Python library for creating beautiful and informative statistical graphics which are based on Matplotlib.
But before we get into moving the legend, let us create a simple seaborn plot with a legend.
import seaborn as sns taxis=sns.load_datset("taxis") graph=sns.scatterplot(data=taxis,x="distance",y="fare",hue="total",palette="YlGnBu") plt.show()
Explanation:
- import seaborn as sns:This code imports Seaborn which is a library in Python that is used for visualising data based on Matplotlib. For creating attractive and informative statistical graphics, Seaborn offers a high-level interface.
- taxis=sns.load_datset(“taxis”):This line loads the “taxis” dataset from Seaborn’s built-in datasets. The sns.load_dataset function fetches the dataset and stores it in the variable taxis. Typically, this dataset has information relating to taxi trips such as distance, fare, total amount.
- graph=sns.scatterplot(data=taxis,x=”distance”,y=”fare”,hue=”total”,palette=”YlGnBu”): This line creates a scatter plot using the Seaborn scatterplot function.
data=taxis: Specifies the DataFrame to use for the plot, which is the “taxis” dataset loaded earlier.
x=”distance”: Sets the “distance” column of the DataFrame as the x-axis.
y=”fare”: Sets “fare” of DataFrame as y-axis
hue=”total”: Colors points according to values in column “total”. Different colors indicate different levels of “total”
palette=”YlGnBu”: Assigns colors from yellow to green to blue for hue using YlGnBu color scheme. - plt.show():The resulting scatter plot shows how far people travel by taxi and how much they pay with markers being coloured based on total charge through an application of colour palette called YlGnBu.
output:
The legends here in the graph represent the total fare of the taxis.
The position of the legends can be anywhere in the plot and is one of the parameter.
Legend Position Parameters
- “best”: Automatically chooses the best location.And is default.
- “upper right”: Places the legend in the top-right corner.
- “upper left”: Places the legend in the top-left corner.
- “lower left”: Places the legend in the bottom-left corner.
- “lower right”: Places the legend in the bottom-right corner.
- “center”: Places the legend in the center of the plot.
- (x, y): Custom coordinates for the legend position.
There are two ways to move seaborn plot legend to a different position in Python.
1.Through legend function:
syntax:
plt.legend(loc="location")
Example:
graph=sns.scatterplot(data=taxis,x="distance",y="fare",hue="total",palette="YlGnBu") graph.legend(loc='upper left') plt.show()
we used ‘loc’ attribute in the legend() function .By default the legends are placed in best position in the plot accordingly .
output:
2.Through move_legend()
Syntax:
sns.move_legend(plt,"location")
Example:
graph=sns.scatterplot(data=taxis,x="distance",y="fare",hue="total",palette="YlGnBu") sns.move_legend(graph,"upper right")
Output:
Some of the links are as below that will throw some insights over seaborn library and related programs:
https://www.codespeedy.com/reverse-the-order-of-a-legend-in-python-seaborn/
Leave a Reply