hey asssitent I am canging a code base form matplotlib to bokeh. Let's start easy.…
- User
hey asssitent I am canging a code base form matplotlib to bokeh. Let's start easy. CHange this: # fig, ax1 = plt.subplots()
y_lim = 2 * DMAX
Decel phase
ax1.plot(x_DMAX, y_DMAX, label=f"Decelartion Phase", linestyle="-")
ax1.scatter(x_DMAX[-1], y_DMAX[-1], color="orange", s=100, marker="*")
ax1.annotate(
"Dmax",
(x_DMAX[-1], y_DMAX[-1]),
textcoords="offset points",
color="orange",
xytext=(0, 10),
ha="center",
fontsize=10,
) Just give me the changed code, not additional values or other stuff
- Assistant
from bokeh.plotting import figure, show from bokeh.models import ColumnDataSource, Label # Prepare data source = ColumnDataSource(data=dict(x=x_DMAX, y=y_DMAX)) # Create a new plot with x and y axis p = figure() # Plot line p.line('x', 'y', source=source, legend_label="Deceleration Phase", line_dash='solid') # Plot scatter point p.scatter(x_DMAX[-1], y_DMAX[-1], color="orange", size=10, marker="asterisk") # Annotate label = Label(x=x_DMAX[-1], y=y_DMAX[-1], text='Dmax', text_color="orange", text_align='center', text_font_size="10pt", x_offset=0, y_offset=10) p.add_layout(label) # Show the plot show(p) - User
Also need to mention I am using streamlit
- Assistant
When integrating Bokeh plots with Streamlit, you can use the
streamlit_bokeh_eventslibrary or simply convert the Bokeh plot into an HTML component for display. Here's how you can modify the code to display it within a Streamlit app:First, ensure you have
streamlitandbokehinstalled:pip install streamlit bokehHere is the modified code:
import streamlit as st from bokeh.plotting import figure from bokeh.models import ColumnDataSource, Label from bokeh.embed import components # Prepare data source = ColumnDataSource(data=dict(x=x_DMAX, y=y_DMAX)) # Create a new plot with x and y axis p = figure(title="Deceleration Phase", plot_width=600, plot_height=400) # Plot line p.line('x', 'y', source=source, legend_label="Deceleration Phase", line_dash='solid') # Plot scatter point p.scatter(x_DMAX[-1], y_DMAX[-1], color="orange", size=10, marker="asterisk") # Annotate label = Label(x=x_DMAX[-1], y=y_DMAX[-1], text='Dmax', text_color="orange", text_align='center', text_font_size="10pt", x_offset=0, y_offset=10) p.add_layout(label) # Use Bokeh components to get the plot elements and insert into Streamlit script, div = components(p) # Display with Streamlit st.write(div, unsafe_allow_html=True) st.write(script, unsafe_allow_html=True) - User
why ColumnDataSource? just tranzition the code from matplolib to bakeh, no extra stuff
- Assistant
Certainly! Here's a more direct translation of your original Matplotlib code to Bokeh, without the use of
ColumnDataSourceor any extra components:import streamlit as st from bokeh.plotting import figure from bokeh.models import Label from bokeh.embed import components # Create a new plot with x and y axis p = figure(title="Deceleration Phase", plot_width=600, plot_height=400) # Plot line p.line(x_DMAX, y_DMAX, legend_label="Deceleration Phase", line_dash='solid') # Plot scatter point p.scatter([x_DMAX[-1]], [y_DMAX[-1]], color="orange", size=10, marker="asterisk") # Annotate label = Label(x=x_DMAX[-1], y=y_DMAX[-1], text='Dmax', text_color="orange", text_align='center', text_font_size="10pt", x_offset=0, y_offset=10) p.add_layout(label) # Embed Bokeh plot into Streamlit script, div = components(p) # Display with Streamlit st.write(div, unsafe_allow_html=True) st.write(script, unsafe_allow_html=True)This modified code sticks closely to your original without introducing additional Bokeh features that were not present in the Matplotlib code. It directly creates a line and a scatter plot, and adds the annotation for "Dmax" as requested.
- User
I also don't think you nee componets just st.plot the p or something
- Assistant
Starting from Streamlit 0.56.0, Streamlit provides a native way to render Bokeh plots using
st.bokeh_chart. This method simplifies embedding Bokeh plots directly into your Streamlit application without the need for converting them to HTML using components. Here is how you can directly integrate a Bokeh figure with Streamlit:import streamlit as st from bokeh.plotting import figure from bokeh.models import Label # Create a new plot with x and y axis p = figure(title="Deceleration Phase", plot_width=600, plot_height=400) # Plot line p.line(x_DMAX, y_DMAX, legend_label="Deceleration Phase", line_dash='solid') # Plot scatter point p.scatter([x_DMAX[-1]], [y_DMAX[-1]], color="orange", size=10, marker="asterisk") # Annotate label = Label(x=x_DMAX[-1], y=y_DMAX[-1], text='Dmax', text_color="orange", text_align='center', text_font_size="10pt", x_offset=0, y_offset=10) p.add_layout(label) # Directly display in Streamlit using st.bokeh_chart st.bokeh_chart(p)This code snippet directly displays the Bokeh figure in Streamlit without any conversion to HTML components, making it straightforward and clean. Just replace and with your actual data.
- User
ok now this # # Infl phase
ax1.plot(
x_SPEED_OF_INFLEXION,
y_SPEED_OF_INFLEXION,
label=f"Inflexion Phase",
linestyle="-",
)
ax1.scatter(
x_SPEED_OF_INFLEXION[-1], y_SPEED_OF_INFLEXION[-1], color="blue", s=100, marker="*"
)
ax1.annotate(
"Inf_Spd",
(x_SPEED_OF_INFLEXION[-1], y_SPEED_OF_INFLEXION[-1]),
textcoords="offset points",
color="blue",
xytext=(0, 10),
ha="center",
fontsize=10,
)
ax1.plot(
[x_SPEED_OF_INFLEXION[-1], x_SPEED_OF_INFLEXION[-1]],
[y_lim, y_SPEED_OF_INFLEXION[-1]],
color="blue",
linestyle=":",
)
- Assistant
Here's how you can translate this portion of the Matplotlib code to use Bokeh in a Streamlit app:
import streamlit as st from bokeh.plotting import figure from bokeh.models import Label # Example y_lim and data y_lim = 2 * DMAX # Adjust DMAX as needed # Create a new plot or reuse the existing figure p = figure(title="Inflexion Phase", plot_width=600, plot_height=400) # Plot line for Inflexion Phase p.line(x_SPEED_OF_INFLEXION, y_SPEED_OF_INFLEXION, legend_label="Inflexion Phase", line_dash='solid') # Plot scatter point p.scatter([x_SPEED_OF_INFLEXION[-1]], [y_SPEED_OF_INFLEXION[-1]], color="blue", size=10, marker="asterisk") # Annotate label_inflexion = Label(x=x_SPEED_OF_INFLEXION[-1], y=y_SPEED_OF_INFLEXION[-1], text='Inf_Spd', text_color="blue", text_align='center', text_font_size="10pt", x_offset=0, y_offset=10) p.add_layout(label_inflexion) # Plot vertical line p.line([x_SPEED_OF_INFLEXION[-1], x_SPEED_OF_INFLEXION[-1]], [y_lim, y_SPEED_OF_INFLEXION[-1]], color="blue", line_dash='dotted') # Display using Streamlit st.bokeh_chart(p) - User
amm that phase should be a continuation of the other. like they are together on the same graph
- Assistant
Got it! To combine both phases on a single Bokeh plot within a Streamlit application, simply continue adding layers onto the same
figureobject. Here's how you can incorporate both the "Deceleration Phase" and "Inflexion Phase" into one plot:import streamlit as st from bokeh.plotting import figure from bokeh.models import Label # Create a new plot with x and y axis p = figure(title="Phases", plot_width=600, plot_height=400) # Deceleration Phase p.line(x_DMAX, y_DMAX, legend_label="Deceleration Phase", line_dash='solid') p.scatter([x_DMAX[-1]], [y_DMAX[-1]], color="orange", size=10, marker="asterisk") label_decel = Label(x=x_DMAX[-1], y=y_DMAX[-1], text='Dmax', text_color="orange", text_align='center', text_font_size="10pt", x_offset=0, y_offset=10) p.add_layout(label_decel) # Inflexion Phase p.line(x_SPEED_OF_INFLEXION, y_SPEED_OF_INFLEXION, legend_label="Inflexion Phase", line_dash='solid') p.scatter([x_SPEED_OF_INFLEXION[-1]], [y_SPEED_OF_INFLEXION[-1]], color="blue", size=10, marker="asterisk") label_inflexion = Label(x=x_SPEED_OF_INFLEXION[-1], y=y_SPEED_OF_INFLEXION[-1], text='Inf_Spd', text_color="blue", text_align='center', text_font_size="10pt", x_offset=0, y_offset=10) p.add_layout(label_inflexion) p.line([x_SPEED_OF_INFLEXION[-1], x_SPEED_OF_INFLEXION[-1]], [y_lim, y_SPEED_OF_INFLEXION[-1]], color="blue", line_dash='dotted') # Display using Streamlit st.bokeh_chart(p) - User
next is this # # Stop phase
ax1.plot(x_STOP, y_STOP, label=f"Stop Phase", linestyle="-")
ax1.scatter(x_GPP[-1], y_GPP[-1], color="green", s=100, marker="*")
ax1.annotate(
"Gpp",
(x_GPP[-1], y_GPP[-1]),
textcoords="offset points",
color="green",
xytext=(0, 10),
ha="center",
fontsize=10,
)
- Assistant
To add the "Stop Phase" to the existing Bokeh plot that already includes the "Deceleration Phase" and "Inflexion Phase," simply continue adding the new elements to the same figure. Here’s how you can do it:
import streamlit as st from bokeh.plotting import figure from bokeh.models import Label # Create a new plot with x and y axis p = figure(title="Phases", plot_width=600, plot_height=400) # Deceleration Phase (existing) p.line(x_DMAX, y_DMAX, legend_label="Deceleration Phase", line_dash='solid') p.scatter([x_DMAX[-1]], [y_DMAX[-1]], color="orange", size=10, marker="asterisk") label_decel = Label(x=x_DMAX[-1], y=y_DMAX[-1], text='Dmax', text_color="orange", text_align='center', text_font_size="10pt", x_offset=0, y_offset=10) p.add_layout(label_decel) # Inflexion Phase (existing) p.line(x_SPEED_OF_INFLEXION, y_SPEED_OF_INFLEXION, legend_label="Inflexion Phase", line_dash='solid') p.scatter([x_SPEED_OF_INFLEXION[-1]], [y_SPEED_OF_INFLEXION[-1]], color="blue", size=10, marker="asterisk") label_inflexion = Label(x=x_SPEED_OF_INFLEXION[-1], y=y_SPEED_OF_INFLEXION[-1], text='Inf_Spd', text_color="blue", text_align='center', text_font_size="10pt", x_offset=0, y_offset=10) p.add_layout(label_inflexion) p.line([x_SPEED_OF_INFLEXION[-1], x_SPEED_OF_INFLEXION[-1]], [y_lim, y_SPEED_OF_INFLEXION[-1]], color="blue", line_dash='dotted') # Stop Phase (new) p.line(x_STOP, y_STOP, legend_label="Stop Phase", line_dash='solid') p.scatter([x_GPP[-1]], [y_GPP[-1]], color="green", size=10, marker="asterisk") label_stop = Label(x=x_GPP[-1], y=y_GPP[-1], text='Gpp', text_color="green", text_align='center', text_font_size="10pt", x_offset=0, y_offset=10) p.add_layout(label_stop) # Display using Streamlit st.bokeh_chart(p) - User
and this # # Stop approach phase
ax1.plot(
x_DECEL_GRADIENT, y_DECEL_GRADIENT, label=f"Stop approach Phase", linestyle="-"
)
ax1.scatter(x_DECEL_GRADIENT[-1], y_DECEL_GRADIENT[-1], color="blue", s=100, marker="*")
ax1.annotate(
"G1s",
(x_DECEL_GRADIENT[-1], y_DECEL_GRADIENT[-1]),
textcoords="offset points",
color="blue",
xytext=(0, 10),
ha="center",
fontsize=10,
)
ax1.plot(
[x_values[-1], x_values[-1]],
[0, y_lim],
color="black",
linestyle=":",
)
- Assistant
- User
ok now this was the rest of the code # ax1.set_ylim(y_lim, -y_lim)
ax1.legend()
ax1.grid(True)
ax2 = ax1.twinx()
ax2.plot(x_values, integral_values, linestyle="-", color="#6699FF")
ax2.set_ylabel("Vehicle Speed [km/h]", color="#6699FF")
ax2.yaxis.set_major_locator(MultipleLocator(5))
ax2.tick_params(axis="y", colors="#6699FF")
st.pyplot(fig)
- Assistant
- User
Hmm this was not really corect, becouse it modyfied my x axis, previously only the new left y axis was modfied like there were 2 speratae x axis for the second graph
- Assistant