首页 > 解决方案 > Python显示excel值图

问题描述

我正在使用此代码->

import xlrd 
import matplotlib.pylab as plt

loc = ("file path") 

wb = xlrd.open_workbook(loc) 
sheet = wb.sheet_by_index(0) 
sheet.cell_value(0, 0) 

for i in range(sheet.nrows): 
    x = sheet.cell_value(i, 0)
    y = sheet.cell_value(i, 1)
    print(x, y) 
    plt.bar(x, y)
plt.xticks(rotation=50, horizontalalignment='right', weight='bold', size='large')
plt.xlabel('Date', weight='bold', size='large')
plt.ylabel('Total Bad Ports', weight='bold', size='large')
plt.show()

从两列 .xls 文件中读取数据并使用其数据显示绘图。我有一个包含两个错误的图表。1. y 轴不显示数值。2. 尝试显示折线图时,我的绘图为空。该图使用 plot.bar 或 plt.plot(x, y, 'ro') 成功(没有 y 轴值)。

*数据形式为

2020 01 28 1900 875
2020 01 29 0700 844
2020 01 29 1300 580
2020 01 29 1900 587
2020 01 30 0700 589
2020 01 30 1300 582

(列 1->2020 01 30 1900、2020 01 29 0700 等和列 2->875、844 等)

提前致谢:)

在此处输入图像描述

标签: pythonexcelplot

解决方案


愚蠢的 1. 问题已解决。.xls 文件中有一个空的起始行,显然不允许正确显示 y 轴值。

尽管我有一个足够好的 plt.bar 图,但我仍然无法使用 plt.plot 命令绘制折线图。任何提示表示赞赏:)


推荐阅读