使用导入的Excel Series在matplotlib中绘图

ffdz8vbo  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(86)

我有一个代码,它会遍历某个Excel表格中的每个图形,代码会吐出每个图形的系列。有没有办法在matplotlib中使用这些系列来绘制它们?

def series_output(chart):
    print(f'\tChart name is {chart.Name}')
    for sc in chart.SeriesCollection():
        series = sc.Formula
        print(series)
       
            
xl = client.Dispatch('Excel.Application')
wb = xl.Workbooks.Open(ef)
sh = wb.Sheets('Calc Tool')  
print(f'Processed sheet {sh.Name}')
for ch in sh.ChartObjects():
    series_output(ch.Chart)
    print('*********************')
    print('                     ')
wb.Close(False)  # don't save & close workbook
xl.Quit()

字符串
示例输出:=SERIES('Ensemble L'! 11块钱,“合奏L”!12元:63元,《合奏》!12加元:63.5加元)

3okqufwl

3okqufwl1#

import re
import matplotlib.pyplot as plt

def series_output(chart):
    print(f'\tChart name is {chart.Name}')
    for sc in chart.SeriesCollection():
        series = sc.Formula
        print(series)
        # Extract data range and values from the series formula
        match = re.search(r"SERIES\('.+?'\!(.+?),(.+?),(.+?),\d+\)", series)
        if match:
            data_range = match.group(2)
            data_values = match.group(3)
            # Process the data range and values to convert them to appropriate formats
            # You may need to adjust this part based on the specific format of your data
            x_values = [float(x.strip()) for x in data_range.split(":")[0][1:].split("$") if x.strip()]
            y_values = [float(y.strip()) for y in data_values.split(":")[0][1:].split("$") if y.strip()]
            # Plot the data using matplotlib
            plt.plot(x_values, y_values, label=chart.Name)
       
    print('*********************')
    print('                     ')
            

xl = client.Dispatch('Excel.Application')
wb = xl.Workbooks.Open(ef)
sh = wb.Sheets('Calc Tool')  
print(f'Processed sheet {sh.Name}')
for ch in sh.ChartObjects():
    series_output(ch.Chart)

# Add legends, labels, and show the plot
plt.legend()
plt.xlabel('X values')
plt.ylabel('Y values')
plt.show()

wb.Close(False)  # don't save & close workbook
xl.Quit()

字符串
您可能需要根据数据的特定格式调整数据处理部分。

相关问题