Python reads excel and draws data curves

part of data

test.xlsx has two columns, the first column is x and the second column is y
Insert image description here

Code:

# coding=utf-8

import matplotlib
matplotlib.use('TkAgg')
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import sys
import xlrd

def read_xlrd(excelFile):
    x=[]
    y=[]
    data = xlrd.open_workbook(excelFile)
    table = data.sheet_by_index(0)
    #print("rows:", table.nrows);

    for rowNum in range(table.nrows):
        rowVale = table.row_values(rowNum)
        x.append(rowVale[0])
        y.append(rowVale[1])
        #print(rowVale[0], rowVale[1])
        #for colNum in range(table.ncols):
            #print(rowVale[colNum])

    return x,y

x,y = read_xlrd(sys.argv[1])

plt.plot(x, y)
plt.grid(True) ##增加格点
plt.show()

implement

python3 show.py ./test.xlsx
Note that the python version here is 3

In addition, matplotlib will have a problem of not displaying, so you need to add matplotlib.use('TkAgg'), otherwise matplotlib's default backend cannot display.

sample graph

Insert image description here
Author: Too handsome to go out

Guess you like

Origin blog.csdn.net/zmlovelx/article/details/116033047#comments_28864873