• streamlit自定义图表大小 (用components渲染pyecharts等)


            streamlit能够很方便地将图表渲染在网页上,所以可以很方便地部署在服务器上。但是它渲染图表时,默认宽高无法改变,这就为用户想更清晰地了解数据情况设置了一定的障碍。

            本文使用components渲染pyecharts图表,从而实现了图表自定义大小。

    先看效果:

            上方折线图是用普通方式添加的,无法自定义宽高,下图是用components渲染的,可以自定义高度。

            第一幅图是streamlit默认的方法添加,代码:

    1. import streamlit_echarts
    2. from pyecharts.charts import Line
    3. from pyecharts import options as opts
    4. columns = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    5. line = Line().add_xaxis(columns)
    6. line.width = '1000px'
    7. data1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
    8. data2 = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
    9. line.add_yaxis(series_name="降水量", y_axis=data1, label_opts=opts.LabelOpts(is_show=True))
    10. line.add_yaxis(series_name="蒸发量", y_axis=data2, label_opts=opts.LabelOpts(is_show=True))
    11. streamlit_echarts.st_pyecharts(
    12. line
    13. )

    这种方法,仅仅是将pyecharts的图当做完整的组件整体添加,效果如下图,比较扁平。 

             而修改后的代码,将pyecharts的折线图,先转换成html文本,然后再用components添加,就能够实现自定义组件的大小了。

    1. import streamlit_echarts
    2. from pyecharts.charts import Line
    3. from pyecharts import options as opts
    4. import streamlit.components.v1 as components #将要展示的 弄成html
    5. line2 = Line().add_xaxis(columns)
    6. line2.height = '550px' # 设置高度
    7. line2.width = '900px'
    8. data1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
    9. data2 = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
    10. line2.add_yaxis(series_name="降水量", y_axis=data1, label_opts=opts.LabelOpts(is_show=True))
    11. line2.add_yaxis(series_name="蒸发量", y_axis=data2, label_opts=opts.LabelOpts(is_show=True))
    12. line2Html = line2.render_embed() #将折线组件转换成html文本
    13. components.html(line2Html,height=900, width=900) #在主页面用streamlit静态组件的方式渲染pyecharts

            效果如下图,很显然,它高了许多。

             使用components渲染pyecharts图表,从而实现了图表自定义大小。

    参考资料:

    https://www.jianshu.com/p/554d64470ec9

    https://pyecharts.org/#/zh-cn/rectangular_charts?id=line:折线面积图

    https://docs.streamlit.io/library/components/components-api

  • 相关阅读:
    xml文件报错 ORA-00907: 缺失右括号
    SAP ABAP教程之 02 创建您的第一份 ABAP 报告 (教程含源码)
    【问一问】消息队列
    读书笔记:多Transformer的双向编码器表示法(Bert)-2
    Kafka【基础入门】
    Nacos服务发现原理分析
    【云原生】k8s 管理平台 rancher
    第十四章 网络管理实战3
    Sentinel基础应用
    QT编程时如何开启c++11?其他IDE如何开启C++11?
  • 原文地址:https://blog.csdn.net/limaning/article/details/126860529