How to change the position of legend in Plotly – Python
In this article, we will learn about what a is legend in the graph and what are the different techniques to change the position of a legend in Plotly – Python. We will also learn about how to change the position of a legend using those techniques.
What is legend?
Legend in the graph is the region in the graph that contains symbols, colors, or lines in the graph. It makes visualizations more easy to interpret. It is generally used when we have multiple categories under particular variables or groups.
What are the techniques to change the position of legend in Plotly?
There are two ways we can change the position of the legend in Plotly:
- Using .Layout()
- Using .update_layout()
Using .Layout()
We use this technique when we use the .go() method to plot a graph in Plotly.
import plotly.graph_objects as go x1 = [10, 20, 30] y1 = [30, 40, 50] x2 = [15, 25, 35] y2 = [35, 45, 55] series_1 = go.Scatter(x=x1, y=y1, mode='markers', name='Series 1') series_2 = go.Scatter(x=x2, y=y2, mode='markers', name='Series 2') plot_data = [series_1,series_2] layout1 = go.Layout(title='Scatter Plot') figure = go.Figure(data=plot_data, layout=layout1) figure.show()
In the above code, first, we are creating dummy data. Then we create two scatter plots i.e. for series_1
and series_2
, and then we are combined two scatter plot objects as plot_data
. Then we defined go.Layout()
where we have not changed the legend’s position yet. Finally, we are creating a figure object using go.Figure()
method. In the given method we are passing parameter values as variables that we have already defined. We are displaying figure using the .show()
method.
Output:
Now we will change the position of the legend in the graph.
x1 = [10, 20, 30] y1 = [30, 40, 50] x2 = [15, 25, 35] y2 = [35, 45, 55] series_1 = go.Scatter(x=x1, y=y1, mode='markers', name='Series 1') series_2 = go.Scatter(x=x2, y=y2, mode='markers', name='Series 2') plot_data = [series_1,series_2] layout1 = go.Layout(title='Scatter Plot with changed position of legend.',legend=dict(x=0.05, y=1, orientation='h')) figure = go.Figure(data=plot_data, layout=layout1) figure.show()
In the above code, we have added parameter values under .Layout() method. Under which we are giving the title and then the legend parameter value we are providing as a dictionary. This dictionary takes values as x, for horizontal position, as y for verticle position, and orientation =’h’, which means the horizontal orientation of the legend. You can change the values of x and y to see changes in position.
Output:
Using update_layout()
When we are using Plotly Express for graphing we use .update_layout() method.
import plotly.express as px import pandas as pd data = {'X': [10, 20, 30, 15, 25, 35], 'Y': [30, 40, 50, 35, 45, 55], 'Series': ['Series 1' if i < 3 else 'Series 2' for i in range(6)]} df = pd.DataFrame(data) fig = px.scatter(df, x='X', y='Y', color='Series',labels={'X Values': 'X', 'Y Values': 'Y'}, title='Scatter Plot') fig.show()
In the above code, first, we are creating dummy data. In the dictionary named ‘data’, ‘Series’ is the variable we are creating to label the data. Then we create a data frame. Finally, we create a scatter plot object and display it using the .show() method.
Output:
import plotly.express as px import pandas as pd data = {'X': [10, 20, 30, 15, 25, 35], 'Y': [30, 40, 50, 35, 45, 55], 'Series': ['Series 1' if i < 3 else 'Series 2' for i in range(6)]} df = pd.DataFrame(data) fig = px.scatter(df, x='X', y='Y', color='Series',labels={'X Values': 'X', 'Y Values': 'Y'}, title='Scatter Plot') fig.update_layout(legend=dict(x=1, y=0, xanchor='right', yanchor='bottom')) fig.show()
We are passing parameters to the legend parameter. It is a dictionary that takes the x
value that shows the horizontal position of the legend, y
value that shows the verticle position of the legend. The ‘anchor’ and ‘anchor’ values describe the position of the area where the legend will be shown.
Leave a Reply