Add labels x axis and y axis for streamlit line_chart
Asked Answered
L

2

10

I would like to add labels for the x and y-axis for my simple line_chart in streamlit.

The plotting command is

st.line_chart(df[["capacity 1", "capacity 2"]])

which plots a line_chart with 2 lines (capacity 1 and capacity 2).

Is there a simple command to add the x and y-axis labels (and maybe a chart title too)?

Lie answered 15/7, 2020 at 18:35 Comment(1)
PS I tried p = figure( title='simple line example', x_axis_label='x', y_axis_label='y') st.line_chart(p, df[["capacity with charging", "capacity without charging"]]) which did not work..Lie
L
1

I think this is not supported by default by Streamlit, st.line_chart() is just a very quick way to call Altair's mark_line() method without any additional argument.

If you want to customize the plot further, you may simply use proper Altair:

import streamlit as st
import altair as alt

chart = (
        alt.Chart(
            data=df,
            title="Your title",
        )
        .mark_line()
        .encode(
            x=alt.X("capacity 1", axis=alt.Axis(title="Capacity 1")),
            x=alt.X("capacity 2", axis=alt.Axis(title="Capacity 2")),
        )
)

st.altair_chart(chart)
Lorindalorine answered 26/8, 2021 at 14:39 Comment(0)
C
0

In the newest version v1.12.0 you could use the arguments x and y to specify what you want to plot from your dataframe. x is:

Column name to use for the x-axis. If None, uses the data index for the x-axis. This argument can only be supplied by keyword.

And y is:

Column name(s) to use for the y-axis. If a sequence of strings, draws several series on the same chart by melting your wide-format table into a long-format table behind the scenes. If None, draws the data of all remaining columns as data series. This argument can only be supplied by keyword.

Here is a reproducible example using the dataset from the link above:

# import packages
import streamlit as st
import pandas as pd

# Example dataframe
df = pd.read_csv('seattle-weather.csv')

# plot
st.line_chart(data = df, x= "date", y = "temp_max")

Output:

enter image description here

As you can see it has labels for the x-axis and y-axis.

Carleecarleen answered 15/9, 2022 at 20:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.