【问题标题】:How to plot different graphs for different sheets from excel workbook using matplotplib?如何使用matplotlib为excel工作簿中的不同工作表绘制不同的图表?
【发布时间】:2019-07-09 14:28:36
【问题描述】:

我有 excel,其中 3 个不同的工作表(Sheet1、Sheet2、Sheet3)各有 2 列 X、Y。

我想为这三个相应的工作表绘制 3 个不同的图表。 如何在 PYthon 中使用 Pandas 和 Matplotlib 绘制它们?

【问题讨论】:

    标签: python excel pandas matplotlib graph


    【解决方案1】:

    pandas 包具有 Excel 读取 绘图功能。一种可能的解决方案如下:

    # Read all sheets of the workbook
    sheets = pd.read_excel("/path/to/your/datafile.xlsx", sheet_name=None)
    for sheet_name, df in sheets.items():
        df.plot(x='x', y='y', title=sheet_name)
    

    这给了我一系列看起来像这张的图像(但带有其他工作表标题 - 我在三张工作表上复制了数据)。

    如果您只想要特定的工作表名称,您可以按照read_excel 调用

    for sheet_name in ("Sheet1", "Sheet2", "Sheet3"):
        df = sheet[sheet_name]
        df.plot(x='x', y='y', title=sheet_name)
    

    编辑:提问者报告有问题的特定文件已使用以下代码成功处理。

    for i in range(3):
       df = pd.read_excel(excel_file, sheet_name=i)
       f = Figure(figsize=(3, 2), dpi=100)
       a = f.add_subplot(111)
       a.plot(df['x'], df['y'], 'ro')
    

    【讨论】:

    • 感谢您的回答!下面的代码是我为在 range(3) 中获取 i 的 3 个不同图形而编写的:df = pd.read_excel(excel_file, sheet_name=i) f = Figure(figsize=(3, 2), dpi=100) a = f。 add_subplot(111) a.plot(df['x'], df['y'], 'ro')
    • 总是很高兴得到积极的反馈,谢谢。我已将您的代码添加到答案中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 2016-01-05
    相关资源
    最近更新 更多