Plotly_events and figure formatting

This is my first post so first of all thanks to the community and everyone helping and contributing!

After searching through the forum and the internet I did not seem to find the answer to my problem:
I am using plotly_events to plot a chart on which I activate a click_event function to get and save data of the point clicked on the graph.
Now the problem is that my app has a sidebar and when creating the chart I can’t format the figure so that it fits in the screen. When the sidebar is open the chart’s right side is out of the screen, but when the sidebar is closed the whole chart is within the screen.
The chart is plotted with:
plotly_events(chart, click_event=True)

On the other hand when the chart is created with st.plotly_chart() it fits perfectly with in the app screen.

I have tried passing st.plotly_chart() to plotly_events( st.plotly_chart(), click_event=True).
which solves the displaying problem but then the click_event is not working.

Does anyone have an idea how to fix this?

Hi @nirco13 ! Thanks for posting your problem. It’s great to hear about your problems.

Can you possibly post the code where it is working and it’s not working? I can try debugging it. Maybe it’s a bug for streamlit and maybe it could be a great find :smiley:



Hi @willhuang,
The app includes an interactive chart that the user can change.
Following is the code where the chart is not fitted to the app screen unless I force it using predefined width and height.

def create_chart(radio, select):

width = ###
height = ###
return fig

with st.sidebar:
radio =…)
select = st.selectbox(…)

fig = create_chart(radio, select)

with st.container:
selected_point = plotly_events(fig, click_event=True)

On the other hand, this code fits the chart perfectly to the app but then the click event is not working:

with st.container:
plotly_events(st.plotly_chart(fig, use container_width=True), click_event=True)

Hey I see this. I will check it out later today when I have time!

@nirco13 , it looks like some of the code is omitted such as plotly_events and create_chart. Would you happen to have that code?

1 Like

plotly_events it is a built-in function of streamlit_plotly_events module
create_chart it is just a function that returns a plotly figure based on data from a dataframe.
Here’s the function structure:

def create_plot():
   fig = make_subplots(specs=[[{"secondary_y":True}]])

   fig.add_trace(go.scattergl(x=df['x'], y=df['y1'], name='y1'), scondary_y=True)
   fig.add_trace(go.scattergl(x=df['x'], y=df['y2'], name='y2'))

   fig.update_layout(title='chart 1', template='plotly_white',
       width=1400, height=900)
   return fig

Hi nicro13, I tried running your code but it looks like im missing some imports. can you also put your imports here? Thanks

Sorry, I probably should have mentioned that I’m using additional modules.
I believe what you are missing is the following import which comes from streamlit_plotly_events module:

from streamlit_plotly_events import plotly_events

Other than that all other imports are pretty standard (streamlit,, plotly.graph_objects)

Hi @nirco13 , sorry for the late reply. what would be the df and go be? Are those packages? Is it possible to copy and paste that is easily reproducible for me? Otherwise I would have to search for all the packages and guess on some of the code.

Usually df stand for a pandas dataframe and go for plotly graphic objects.
Instead of the df you can use simple arrays like np.array([1,2,3]).
The goal here is to understand how to use the streamlit keyword use_container_width within the community component plotly_events .