You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Streamlit的line_chart添加X轴与Y轴标签?

为Streamlit折线图添加X/Y轴标签的解决方案

方法一:基于Altair实现(推荐)

st.line_chart本质是Altair图表的简化封装,直接使用Altair可以灵活自定义轴标签,同时保留原生交互特性。示例代码如下:

import streamlit as st
import altair as alt

# 请根据你的DataFrame列名替换示例中的字段
chart = alt.Chart(df).mark_line().encode(
    x=alt.X('你的X轴列名:T', title='X轴标签文本'),  # 时间类型用:T,数值类型用:Q
    y=alt.Y('你的Y轴列名:Q', title='Y轴标签文本'),
    tooltip=['你的X轴列名', '你的Y轴列名']  # 可选,添加悬浮提示信息
).interactive()

st.altair_chart(chart, use_container_width=True)

方法二:使用Matplotlib整合到Streamlit

如果习惯Matplotlib的语法,可通过以下方式正确渲染带轴标签的图表:

import streamlit as st
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
# 替换为你的DataFrame数据,比如df.index作为X轴,df['数值列']作为Y轴
ax.plot(df.index, df['数值列'])
ax.set_xlabel('X轴标签文本')
ax.set_ylabel('Y轴标签文本')

st.pyplot(fig)

内容的提问来源于stack exchange,提问作者Jamiu S.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 03:15:57