7

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)?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
user3550552
  • 167
  • 1
  • 13
  • 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.. – user3550552 Jul 15 '20 at 18:38

2 Answers2

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)
arnaud
  • 3,293
  • 1
  • 10
  • 27
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.

Quinten
  • 35,235
  • 5
  • 20
  • 53