Open In App

How to plot on secondary Y-Axis with Plotly Express in Python?

Last Updated : 06 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Perquisites: Python Plotly

In this article, we will learn how to plot on secondary y-Axis with plotly express. 

One of the most deceptively-powerful features of Plotly data visualization is the ability for a viewer to quickly analyze a sufficient amount of information about data when pointing the cursor over the point label appears. It provides us the easiest way to read and analyze information from the document. Multi-Axis charts are used to plot data points from a query along two or more axes. Here we will discuss different methods to plot multiple y-axis in plotly express to make it more clear.

Syntax: yaxis=dict(title=”yaxis title”, overlaying=”y”, side=”left”, position=[0-1])

Parameters:

  • title: str, (name of the axis)
  • overlaying: str, (name of the bars are plotted over one another)
  • side: [‘top’, ‘bottom’, ‘left’, ‘right’]
  • position: int/float, [in range between 0-1].

Example 1: Two Y-Axis

In this example, we are taking two data plots, the first one is Bar chart and another is a Scatter chart. Update_layout the properties of the figure’s layout with a dict and/or with keyword arguments, In which we will define consecutive secondary yaxis (i.e yaxis, yaxis2 ) with the help of the above-given syntax for each axis.

Python3




import plotly.graph_objects as go
 
fig = go.Figure()
 
fig.add_trace(go.Bar(x=[1, 2, 3], y=[40, 50, 60],
                     name="yaxis1 data", yaxis='y'))
 
fig.add_trace(go.Scatter(x=[2, 3, 4], y=[4, 5, 6],
                      name="yaxis2 data", yaxis="y2"))
 
# Create axis objects
fig.update_layout(xaxis=dict(domain=[0.3, 0.7]),
    #create 1st y axis             
    yaxis=dict(
        title="yaxis title",
        titlefont=dict(color="#1f77b4"),
        tickfont=dict(color="#1f77b4")),
                   
    #create 2nd y axis      
    yaxis2=dict(title="yaxis2 title",overlaying="y",
                side="left",position=0.15))
 
# title
fig.update_layout(
    title_text="Geeksforgeeks - Three y-axes",
    width=800,
)
 
fig.show()


 
 

Output:

 

Example 2: Three Y-axis

 

In this example, we are taking three data plots, the first one is Bar chart and another is a Scatter chart and the last one is another Bar Chart. Update_layout the properties of the figure’s layout with a dict and/or with keyword arguments. In which we will define consecutive secondary y-axis (i.e yaxis, yaxis2, yaxis3… ) with the help of the above-given syntax for each axis

 

Python3




import plotly.graph_objects as go
 
fig = go.Figure()
 
fig.add_trace(go.Bar(x=[1, 2, 3], y=[4, 5, 6],
                     name="yaxis1 data", yaxis='y'))
 
fig.add_trace(go.Scatter(x=[2, 3, 5], y=[40, 50, 60],
                         name="yaxis2 data", yaxis="y2"))
 
fig.add_trace(go.Bar(x=[4, 5, 6], y=[40000, 50000, 60000],
                     name="yaxis3 data", yaxis="y3"))
 
# Create axis objects
fig.update_layout(xaxis=dict(domain=[0.3, 0.7]),
 
                  # create 1st y axis
                  yaxis=dict(
    title="yaxis title",
    titlefont=dict(color="#1f77b4"),
    tickfont=dict(color="#1f77b4")),
 
    # create 2nd y axis
    yaxis2=dict(title="yaxis2 title", overlaying="y",
                side="left", position=0.15),
 
    # create 3rd y axis
    yaxis3=dict(
        title="yaxis3 title",
        anchor="x", overlaying="y", side="right"))
 
 
# title
fig.update_layout(
    title_text="Geeksforgeeks - Three y-axes",
    width=800,
)
 
fig.show()


Output:

Example 3: Four Y-axis

In this example, we are taking two data plots of Bar charts and two plots of Scatters charts. Update_layout the properties of the figure’s layout with a dict and/or with keyword arguments. In which we will define consecutive secondary y-axis (i.e yaxis, yaxis2, yaxis3, yaxis4 ) with the help of above-given syntax for each axis

Python3




import plotly.graph_objects as go
 
fig = go.Figure()
 
fig.add_trace(go.Bar(x=[1, 2, 3], y=[4, 5, 6],
                     name="yaxis1 data"))
 
fig.add_trace(go.Scatter(x=[2, 3, 4], y=[40, 50, 60],
                         name="yaxis2 data", yaxis="y2"))
 
fig.add_trace(go.Scatter(x=[4, 5, 6],
                         y=[40000, 50000, 60000],
                         name="yaxis3 data", yaxis="y3"))
 
fig.add_trace(go.Bar(
    x=[5, 6, 7], y=[400000, 500000, 600000],
    name="yaxis4 data", yaxis="y4"))
 
 
# Create axis objects
fig.update_layout(
    xaxis=dict(
        domain=[0.3, 0.7]
    ),
    yaxis=dict(
        title="yaxis title", titlefont=dict(color="#1f77b4"),
        tickfont=dict(color="#1f77b4")),
 
    yaxis2=dict(
        title="yaxis2 title",
        titlefont=dict(color="#ff7f0e"),
        tickfont=dict(color="#ff7f0e"),
        anchor="free", overlaying="y",
        side="left", position=0.15),
 
    yaxis3=dict(
        title="yaxis3 title",
        titlefont=dict(color="#d62728"),
        tickfont=dict(color="#d62728"),
        anchor="x", overlaying="y", side="right"),
 
    yaxis4=dict(
        title="yaxis4 title",
        titlefont=dict(color="#9467bd"),
        tickfont=dict(color="#9467bd"),
        anchor="free", overlaying="y",
        side="right", position=0.85)
)
 
# Update layout properties
fig.update_layout(
    title_text="Four y-axes",
    width=800,
)
 
fig.show()


Output:



Similar Reads

How to Add a Y-Axis Label to the Secondary Y-Axis in Matplotlib?
Sometimes while analyzing any data through graphs we need two x or y-axis to get some more insights into the data. Matplotlib library of Python is the most popular data visualization library, and we can generate any type of plot in Matplotlib. We can create a plot that has two y-axes and can provide different labels to both of the y-axis. We can ma
4 min read
How to hide legend with Plotly Express and Plotly in Python?
In this article, we will learn How to hide legend with Plotly Express and Plotly. Here we will discuss two different methods for hiding legend in plotly and plotly express, using two different examples for each to make it more clear. Syntax: For legend: fig.update_traces(showlegend=False)fig.update(layout_showlegend=False) Example 1: In this exampl
2 min read
Plotly - How to show legend in single-trace scatterplot with plotly express?
In this article let's see how to show legend in single-trace scatterplot with plotly express. A 'trace' is the name given to each plot inside the chart. Generally in plotly legend is not visible for single trace scatter plots. Example: In the below, example packages and data are imported and a single trace scatter plot is plotted using px.scatter()
1 min read
How to hide axis titles in plotly express figure with facets in Python?
In this article, we will learn how to hide axis titles in a plotly express figure with facets in Python. We can hide the axis by setting the axis title as blank by iterating through for loop. We are hiding the axis only for X-axis and Y-axis so we have to compare this condition in each iteration Syntax: for axis in fig.layout: if type(fig.layout[ax
2 min read
Python Plotly: How to set the range of the y axis?
In this article, we will learn how to set the range of the y-axis of a graph using plotly in Python. To install this module type the below command in the terminal: pip install plotly Example 1: Using layout_yaxis_range as a parameter In this example, we have first import the required libraries i.e pandas,numpy and plotly.objs, then we generated som
3 min read
Plot Live Graphs using Python Dash and Plotly
Dash is a Python framework built on top of ReactJS, Plotly and Flask. It is used to create interactive web dashboards using just python. Live graphs are particularly necessary for certain applications such as medical tests, stock data, or basically for any kind of data that changes in a very short amount of time where it is not viable to reload eac
4 min read
Pie plot using Plotly in Python
Plotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library. Pie Plot A pie chart is a circular analytical ch
3 min read
Box Plot using Plotly in Python
Plotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library. Box Plot A box plot is a demographic representat
8 min read
How to Create Stacked area plot using Plotly in Python?
Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library. Stacked Area plot A stacked area chart is the am
2 min read
3D scatter plot using Plotly in Python
Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library. 3D Scatter plot in Plotly A scatterplot can be u
3 min read
Practice Tags :