用python从excel表中绘制的图形单元格值(共225000个+单元格)作为x轴和y轴图形必须在同一excel sh中

2024-04-26 13:03:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须自动生成excel工作表报表。 我使用python编写脚本。 要求是对一个巨大的数据进行解析,按照要求的特定格式,然后绘制F列和G列的图形(这些列总共有222.5万个单元格)。但是,当使用pandas和got.png输出时,图形绘制不准确!!!。(看到一些根本不在excel单元格中的值-(假设最大值为300,但绘制的图形有1000!!) 使用matplotlib时,只有在给定99999值时才能看到绘图!! 当给出完整的F和G列时,还有一个空白的图!!你知道吗

我可以将数据导入到一本书的特定excel表中,但无法绘制图表。 使用了pandas、xlsxwriter和matplotlib。你知道吗

下面是我尝试过的一段代码:

def throughput_freespace(path):
    path=path.replace('"','')
    df=pd.read_excel(path)
    y_list=((1/(df['Net Latency (ms)']/1000))/2)
    res=len(y_list)
    for i in range(res):
        try:
            y_list[i]=int(round(float(y_list[i])))
        except:
            pass
    total_space=(res)*512
    x_list=[]
    num=float(total_space/1024.0)
    x_list.append(num)
    for j in range(1,res):
        num-=0.5
        x_list.append(num)
    df['Free Space (MB)']=x_list
    df['MB/s']=y_list
    ts_list=[]
    ts_list.append(total_space)

    for i in range(1,res):
        ts_list.append('')

    df['Total Free Space(KB)']=ts_list
    df.at[1,'K']=total_space+512    
    df.to_excel(path)
    writer = pd.ExcelWriter(path, engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')

    workbook = writer.book
    worksheet = writer.sheets['Sheet1']

    chart1 = workbook.add_chart({'type': 'scatter',

                                'subtype':'smooth'})
    print 'Creating Chart\n'
    for i in range(1, len(df.columns) + 1):
        chart1.add_series({
            'name' : ['Sheet1', 0, i],
            'values' : ['Sheet1', 1, i, len(df.index), i],
            'categories': ['Sheet1', 1, 0, len(df.index), 0],
            'line':       {'color': 'red'},
            'gap': 50

        })    
    chart1.add_series({
        'categories': '=Sheet1!$F$1:$F$99999',
        'values':     '=Sheet1!$G$1:$G$99999',
        'line':       {'color': 'red'},
    })
    chart1.set_title ({'name': 'Results of sample analysis'})
    chart1.set_x_axis({'name': 'Test number'})
    chart1.set_y_axis({'name': 'Sample length (mm)'})

    # Set an Excel chart style.
    chart1.set_style(11)

    # Insert the chart into the worksheet (with an offset).
    worksheet.insert_chart('L15', chart1, {'x_offset': 25, 'y_offset': 10}) 
    workbook.close()
    print 'All Good'

    list1=[]
    list2=[]
    for i in range(res):
        list1.append(int(x_list[i]))
        try:
            list2.append(int(y_list[i]))
        except:
            pass
    print list2

    plot_graph(list1,list2,1)
    ax = df.plot("Free Space (MB)", "MB/s")

    ax.figure.autofmt_xdate()
    plt.xlabel('Free Space(MB)') 
    plt.ylabel('MB/s') 
    plt.title('Throughput')
    plt.savefig('testplot1.png')    

    plt.show()

    #plt.title('My first graph!') 
    #plt.savefig('testplot.png') 
    print ('Done')

expected输出是一个趋势线平滑图,它将2225000个可用空间(MB)值绘制为MB/s(同样是2225000个单元格值) 有没有更好的选择,或者有没有更好更简单的方法用上面巨大的单元格值在excel表中绘制图表??!!。 提前谢谢。你知道吗


Tags: pathnameindffor绘制rangeres