Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly: How to plot on secondary y-Axis with plotly express

How do I utilize plotly.express to plot multiple lines on two yaxis out of one Pandas dataframe?

I find this very useful to plot all columns containing a specific substring:

    fig = px.line(df, y=df.filter(regex="Linear").columns, render_mode="webgl")

as I don't want to loop over all my filtered columns and use something like:

    fig.add_trace(go.Scattergl(x=df["Time"], y=df["Linear-"]))

in each iteration.

like image 461
derflo Avatar asked Jul 11 '20 19:07

derflo


People also ask

How do you change the y-axis in plotly?

Tick Labels You have the option of showing these labels or hiding them. Go to 'Axes', then 'Tick Labels' and depending on the axis you want to change, select X, Y, or 'ALL'.

How do you plot multiple Y-axis in Python?

Using subplots() method, create a figure and a set of subplots. Plot [1, 2, 3, 4, 5] data points on the left Y-axis scales. Using twinx() method, create a twin of Axes with a shared X-axis but independent Y-axis, ax2. Plot [11, 12, 31, 41, 15] data points on the right Y-axis scale, with blue color.

What is the difference between plotly and Plotly Express?

Overview. The plotly. express module (usually imported as px ) contains functions that can create entire figures at once, and is referred to as Plotly Express or PX. Plotly Express is a built-in part of the plotly library, and is the recommended starting point for creating most common figures.

How do I change the Y-axis scale in Python plotly?

Scatter() function to make a scatter plot. The go. Figure() function takes in data as input where we set the mode as 'lines' using mode='lines'. We have used the magic underscore notation i.e layout_yaxis_range=[-8,8] to set the y-axis range from -8 to 8.

How do I plot multiple y axes in Plotly Express?

Currently, Plotly Express does not support multiple Y axes on a single figure. So, we shall use Plotly go. Plotly provides a function called make_subplots () to plot charts with multiple Y – axes . plotly.subplots.make_subplots (rows=1, cols=1, specs=None)

What is the use of Plotly Express?

Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data and produces easy-to-style figures. Note: At this time, Plotly Express does not support multiple Y axes on a single figure.

How do I make subplots in Plotly Express?

Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data and produces easy-to-style figures. Note: At this time, Plotly Express does not support multiple Y axes on a single figure. To make such a figure, use the make_subplots () function in conjunction with graph objects as documented below.

How to add lines straight to secondary_y in Plotly Express?

Plotly express does not support adding lines straight to secondary_y by now. Therefore the indirect approach as shown in my answer below. I will clarify this in the question soon. For one very familiar with plotly this might be a nobrainer.


1 Answers

It took me some time to fiddle this out, but I feel this could be useful to some people.

# import some stuff
import plotly.express as px
from plotly.subplots import make_subplots
import pandas as pd
import numpy as np

# create some data
df = pd.DataFrame()
n = 50
df["Time"] = np.arange(n)
df["Linear-"] = np.arange(n)+np.random.rand(n)
df["Linear+"] = np.arange(n)+np.random.rand(n)
df["Log-"] = np.arange(n)+np.random.rand(n)
df["Log+"] = np.arange(n)+np.random.rand(n)
df.set_index("Time", inplace=True)

subfig = make_subplots(specs=[[{"secondary_y": True}]])

# create two independent figures with px.line each containing data from multiple columns
fig = px.line(df, y=df.filter(regex="Linear").columns, render_mode="webgl",)
fig2 = px.line(df, y=df.filter(regex="Log").columns, render_mode="webgl",)

fig2.update_traces(yaxis="y2")

subfig.add_traces(fig.data + fig2.data)
subfig.layout.xaxis.title="Time"
subfig.layout.yaxis.title="Linear Y"
subfig.layout.yaxis2.type="log"
subfig.layout.yaxis2.title="Log Y"
# recoloring is necessary otherwise lines from fig und fig2 would share each color
# e.g. Linear-, Log- = blue; Linear+, Log+ = red... we don't want this
subfig.for_each_trace(lambda t: t.update(line=dict(color=t.marker.color)))
subfig.show()

Finished graph

The trick with

subfig.for_each_trace(lambda t: t.update(line=dict(color=t.marker.color)))

I got from nicolaskruchten here: https://stackoverflow.com/a/60031260

like image 195
derflo Avatar answered Sep 17 '22 15:09

derflo