Home > Software design >  How do I show different yaxis value in a subplot?
How do I show different yaxis value in a subplot?

Time:02-18

I want to show different ranges in the first row and different ranges in the second row? For instance first row can show up to 50 and second can show up to 100?

from plotly.subplots import make_subplots
import plotly.graph_objects as go
import plotly.express as px
import numpy as np
import pandas as pd

# set seed
np.random.seed(41)

#create three different normally distributed datasets
score_array_A = np.random.normal(size = 100, loc = 15, scale=5)
score_array_B = np.random.normal(size = 200, loc = 50, scale=10)
score_array_C = np.random.normal(size = 300, loc = 70, scale=15)

#turn normal arrays into dataframes
#score_data['T(s)']
score_df_A = pd.DataFrame({'T(s)':score_array_A,'D':'2'})
score_df_B = pd.DataFrame({'T(s)':score_array_B,'D':'3'})
score_df_C = pd.DataFrame({'T(s)':score_array_C,'D':'4'})

#concat dataframes together
score_data = pd.concat([score_df_A,score_df_B,score_df_C])

score_data = score_data.assign(Req = np.where(score_data['T(s)']%5 > 1, "1", "5"))

#to plot subplots
px.box(data_frame = score_data
             ,y = 'T(s)'
             ,facet_col = 'D'
             , facet_row = 'Req'
             ,facet_col_wrap = 0, 
               template='simple_white',
               width=600, 
               height=300
             )

CodePudding user response:

After creating the figure using Plotly Express update each yaxis so that it is not configured to match the primary yaxis. Have also updated showticklabels

import plotly.express as px
import numpy as np
import pandas as pd

# set seed
np.random.seed(41)

# create three different normally distributed datasets
score_array_A = np.random.normal(size=100, loc=15, scale=5)
score_array_B = np.random.normal(size=200, loc=50, scale=10)
score_array_C = np.random.normal(size=300, loc=70, scale=15)

# turn normal arrays into dataframes
# score_data['T(s)']
score_df_A = pd.DataFrame({"T(s)": score_array_A, "D": "2"})
score_df_B = pd.DataFrame({"T(s)": score_array_B, "D": "3"})
score_df_C = pd.DataFrame({"T(s)": score_array_C, "D": "4"})

# concat dataframes together
score_data = pd.concat([score_df_A, score_df_B, score_df_C])

score_data = score_data.assign(Req=np.where(score_data["T(s)"] % 5 > 1, "1", "5"))

# to plot subplots
fig = px.box(
    data_frame=score_data,
    y="T(s)",
    facet_col="D",
    facet_row="Req",
    facet_col_wrap=0,
    template="simple_white",
    width=600,
    height=300,
)

fig.update_layout(
    {
        yax: {"matches": None, "showticklabels": True}
        for yax in fig.to_dict()["layout"].keys()
        if "yaxis" in yax
    }
)

enter image description here

  • Related