The following code only displays the last chart, but not the first three. I tried adapting both parent and item layout, but could not get it to work.
import plotly.express as px
import ipywidgets as widgets
from ipywidgets import GridspecLayout, Layout, Output
com_lay = Layout(
width='100%',
height='300px',
border='1px solid gray',
overflow='auto'
)
# Create output widgets
out_bar = Output(layout=com_lay)
out_line = Output(layout=com_lay)
out_scat = Output(layout=com_lay)
out_hist = Output(layout=com_lay)
# Load and show plots
df = px.data.iris()
with out_scat:
fig = px.scatter(df, x='sepal_width', y='sepal_length', color='species')
fig.show()
df_tips = px.data.tips()
with out_bar:
fig = px.bar(df_tips, x='day', y='total_bill', color='sex', barmode='group')
fig.show()
df_line = px.data.gapminder().query("country == 'Canada'")
with out_line:
fig = px.line(df_line, x='year', y='gdpPercap', title='GDP per Capita - Canada')
fig.show()
with out_hist:
fig = px.histogram(df_tips, x='total_bill', nbins=30, color='sex')
fig.show()
# Set up grid layout
grid = GridspecLayout(
4, 1,
layout=Layout(
width='100%',
height='auto',
grid_gap='10px'
)
)
# Assign plots to grid
grid[0, 0] = out_line
grid[1, 0] = out_bar
grid[2, 0] = out_scat
grid[3, 0] = out_hist
grid
How can I get all four charts to display?
I think problem makes .show()
. It may remove previous figure(s).
When I remove .show()
for last figure then I see only previous figure., etc.
If I use display(fig)
then it makes the same problem.
For me works display(fig1, fig2, fig3, fig4)
at the end, even without grid
import plotly.express as px
import ipywidgets as widgets
from ipywidgets import GridspecLayout, Layout, Output
com_lay = Layout(
width='100%',
height='300px',
border='1px solid gray',
overflow='auto'
)
# Create output widgets
out_bar = Output(layout=com_lay)
out_line = Output(layout=com_lay)
out_scat = Output(layout=com_lay)
out_hist = Output(layout=com_lay)
# Load and show plots
df = px.data.iris()
with out_scat:
fig1 = px.scatter(df, x='sepal_width', y='sepal_length', color='species')
#fig1.show() # it seems it removes previous show()
#display(fig1) # it seems it removes previous display()
df_tips = px.data.tips()
with out_bar:
fig2 = px.bar(df_tips, x='day', y='total_bill', color='sex', barmode='group')
#fig2.show() # it seems it removes previous show()
#display(fig2) # it seems it removes previous display()
df_line = px.data.gapminder().query("country == 'Canada'")
with out_line:
fig3 = px.line(df_line, x='year', y='gdpPercap', title='GDP per Capita - Canada')
#fig3.show() # it seems it removes previous show()
#display(fig3) # it seems it removes previous display()
with out_hist:
fig4 = px.histogram(df_tips, x='total_bill', nbins=30, color='sex')
#fig4.show() # it seems it removes previous show()
#display(fig4) # it seems it removes previous display()
# Set up grid layout
grid = GridspecLayout(
4, 1,
layout=Layout(
width='100%',
height='auto',
grid_gap='10px'
)
)
# Assign plots to grid
grid[0, 0] = out_line
grid[1, 0] = out_bar
grid[2, 0] = out_scat
grid[3, 0] = out_hist
#grid
display(fig1, fig2, fig3, fig4) # works here