Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly Log Scale in Subplot Python

Tags:

python

plotly

I have a 3 columns in my dataframe. I have charted them all in plotly, and the below code puts them side by side in a subplot. I would like to change the third chart 'c' to have a logarithmic scale. Is this possible?

fig = make_subplots(rows=1, cols=3)
fig.add_trace(
    go.Scatter(x = df.index,y = df['a'],mode = 'lines+markers',name = 'Daily')

fig.add_trace(
    go.Scatter(x = df.index,y = df['b'],mode = 'lines+markers',name = 'Total'    )

fig.add_trace(
    go.Scatter(x = df.index,y = df['c'],mode = 'lines+markers',name = 'Total'
)


fig.layout()
fig.update_layout(height=600, width=800, title_text="Subplots")
fig.show()
like image 870
fred.schwartz Avatar asked Apr 05 '20 11:04

fred.schwartz


1 Answers

See the section on "Customizing Subplot Axes" in the Plotly documentation. I included an example below for 2 subplots, but the logic is the same regardless of the number of subplots.

import plotly.graph_objects as go
from plotly.subplots import make_subplots

fig = make_subplots(rows=1, cols=2, subplot_titles=("Default Scale", "Logarithmic Scale"))

# subplot in default scale
fig.add_trace(go.Scatter(x=[0.1, 0.2, 0.3, 0.4, 0.5], y=[1.105, 1.221, 1.35, 1.492, 1.649]), row=1, col=1)
fig.update_xaxes(title_text="x-axis in default scale", row=1, col=1)
fig.update_yaxes(title_text="y-axis in default scale", row=1, col=1)

# subplot in logarithmic scale
fig.add_trace(go.Scatter(x=[0.1, 0.2, 0.3, 0.4, 0.5], y=[1.105, 1.221, 1.35, 1.492, 1.649]), row=1, col=2)
fig.update_xaxes(title_text="x-axis in logarithmic scale", type="log", row=1, col=2)
fig.update_yaxes(title_text="y-axis in logarithmic scale", type="log", row=1, col=2)

fig.update_layout(showlegend=False)

fig.show()

enter image description here

like image 61
Flavia Giammarino Avatar answered Nov 09 '22 14:11

Flavia Giammarino