openpyxl3.0官方文档(9)—— 条形图和柱形图

在条形图中,值以水平条或垂直列的形式展示。

垂直、水平和堆叠条形图¶

下列设置影响不同的图表类型:

  • 通过分别将type设置为colbar,在垂直条形图和水平条形图之间切换。
  • 使用堆叠图表时,需要将overlap设置为100。
  • 如果条是水平的,则x轴和y轴是相反的。
    在这里插入图片描述下面代码将生成上面四个图表。
    from openpyxl import Workbook
    from openpyxl.chart import BarChart, Series, Reference
    
    wb = Workbook(write_only=True)
    ws = wb.create_sheet()
    
    rows = [
        ('Number', 'Batch 1', 'Batch 2'),
        (2, 10, 30),
        (3, 40, 60),
        (4, 50, 70),
        (5, 20, 10),
        (6, 10, 40),
        (7, 50, 30),
    ]
    
    
    for row in rows:
        ws.append(row)
    
    
    chart1 = BarChart()
    chart1.type = "col"
    chart1.style = 10
    chart1.title = "Bar Chart"
    chart1.y_axis.title = 'Test number'
    chart1.x_axis.title = 'Sample length (mm)'
    
    data = Reference(ws, min_col=2, min_row=1, max_row=7, max_col=3)
    cats = Reference(ws, min_col=1, min_row=2, max_row=7)
    chart1.add_data(data, titles_from_data=True)
    chart1.set_categories(cats)
    chart1.shape = 4
    ws.add_chart(chart1, "A10")
    
    from copy import deepcopy
    
    chart2 = deepcopy(chart1)
    chart2.style = 11
    chart2.type = "bar"
    chart2.title = "Horizontal Bar Chart"
    
    ws.add_chart(chart2, "G10")
    
    
    chart3 = deepcopy(chart1)
    chart3.type = "col"
    chart3.style = 12
    chart3.grouping = "stacked"
    chart3.overlap = 100
    chart3.title = 'Stacked Chart'
    
    ws.add_chart(chart3, "A27")
    
    
    chart4 = deepcopy(chart1)
    chart4.type = "bar"
    chart4.style = 13
    chart4.grouping = "percentStacked"
    chart4.overlap = 100
    chart4.title = 'Percent Stacked Chart'
    
    ws.add_chart(chart4, "G27")
    
    wb.save("bar.xlsx")
    

三维条形图¶

也可以创建三维条形图

    from openpyxl import Workbook
    from openpyxl.chart import (
        Reference,
        Series,
        BarChart3D,
    )
    
    wb = Workbook()
    ws = wb.active
    
    rows = [
        (None, 2013, 2014),
        ("Apples", 5, 4),
        ("Oranges", 6, 2),
        ("Pears", 8, 3)
    ]
    
    for row in rows:
        ws.append(row)
    
    data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=4)
    titles = Reference(ws, min_col=1, min_row=2, max_row=4)
    chart = BarChart3D()
    chart.title = "3D Bar Chart"
    chart.add_data(data=data, titles_from_data=True)
    chart.set_categories(titles)
    
    ws.add_chart(chart, "E5")
    wb.save("bar3d.xlsx")
    

这将生成一个简单的三维条形图。
在这里插入图片描述

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Streamlit 中,使用 Pandas 库可以轻松地将 Excel 文件读取为 DataFrame。然后,可以使用 Matplotlib 或 Plotly 库将 DataFrame 转换为折线图、条形图柱形图。 以下是一个简单的示例代码,将 Excel 文件转换为折线图: ```python import streamlit as st import pandas as pd import matplotlib.pyplot as plt # 上传 Excel 文件 file = st.file_uploader("Upload Excel file", type=["xlsx", "xls"]) if file: # 读取 Excel 文件为 DataFrame df = pd.read_excel(file) # 绘制折线图 fig, ax = plt.subplots() ax.plot(df["x"], df["y"]) ax.set_xlabel("X") ax.set_ylabel("Y") st.pyplot(fig) ``` 对于条形图柱形图,可以使用 Matplotlib 或 Plotly 库。以下是一个简单的示例代码,将 Excel 文件转换为条形图: ```python import streamlit as st import pandas as pd import matplotlib.pyplot as plt # 上传 Excel 文件 file = st.file_uploader("Upload Excel file", type=["xlsx", "xls"]) if file: # 读取 Excel 文件为 DataFrame df = pd.read_excel(file) # 绘制条形图 fig, ax = plt.subplots() ax.bar(df["x"], df["y"]) ax.set_xlabel("X") ax.set_ylabel("Y") st.pyplot(fig) ``` 以下是一个简单的示例代码,将 Excel 文件转换为柱形图: ```python import streamlit as st import pandas as pd import plotly.express as px # 上传 Excel 文件 file = st.file_uploader("Upload Excel file", type=["xlsx", "xls"]) if file: # 读取 Excel 文件为 DataFrame df = pd.read_excel(file) # 绘制柱形图 fig = px.bar(df, x="x", y="y") st.plotly_chart(fig) ``` 注意:在使用 Plotly 库时,需要安装 Plotly 和 Plotly Express。可以使用以下命令进行安装: ``` pip install plotly pip install plotly-express ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值