Python3操作excel制作数据图形

通过将数据写入到excel里,然后读取制作图形

import csv
from itertools import islice

from matplotlib import pyplot as plt         #导入图形包制作图形

itable = [['1', 'sz', 11, 111, 1111], ['2', 'sz', 22, 222, 2222], ['3', 'sz', 33, 333, 3333],
          ['4', 'sz', 44, 444, 4444]]

#将数据写入到excel里
with open('csvtest.csv', 'w', newline='') as csvf:
    swriter = csv.writer(csvf, dialect='excel')
    swriter.writerow(['id', 'name', 'age', 'salary', 'date'])

    swriter.writerows(itable)



highs, lows = [],[]
with open('csvtest.csv') as csvf:
    readers = csv.reader(csvf)

    for line in islice(readers, 1, None):
        high = int(line[2])
        highs.append(high)      #数据追加储存

        low = int(line[3])
        lows.append(low)       #数据追加储存


#制作图形,并显示
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(highs, c='red', alpha=0.5)
plt.plot(lows, c='yellow', alpha=1)
plt.fill_between(highs, lows, facecolor='blue', alpha=0.1)
plt.show()

你可能感兴趣的:(Python-练习题)