脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python读取excel数据并且画图的实现示例

python读取excel数据并且画图的实现示例

2021-09-04 00:23TR_Goldfish Python

这篇文章主要介绍了python读取excel数据并且画图的实现示例,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下

一,要读取的数据的格式:

python读取excel数据并且画图的实现示例

二,数据读取部分:

b站视频参考:https://www.bilibili.com/video/bv14c4y1w7nj?t=148

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 1930
workbook=xlrd.open_workbook('1930.xlsx')
sheet= workbook.sheet_by_index(0)
a1=[]
b1=[]
# sheet.cell_value(i,0):第i行的第0个元素
for i in range(1,sheet.nrows):
 a1.append(sheet.cell_value(i,0))
 b1.append(sheet.cell_value(i,1))
 
if len(a1)!=len(b1):
 print("false")
drawbar(a1,b1,1930)

三,画图函数

1. def drawbar(music_genre,singer_num,year)

参数介绍

 

参数名 参数含义
music_genre 音乐流派名称list
singer_num 音乐流派对应音乐家数量list
year 读的文件的年份(因为源代码是从1840到2020的)

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def drawbar(music_genre,singer_num,year):
 arr_len=len(music_genre)
 # 由循环得到一个字典,key是音乐流派,value是这个音乐流派对应的音乐家的数量
 i=0
 dict_music_singer={}
 while i<arr_len:
 dict_music_singer[music_genre[i]]=singer_num[i]
 i=i+1
 
    # 注释1
 pyplot.bar(x=0, bottom=range(arr_len), height=0.3, width=singer_num, orientation="horizontal")
 # 注释2
 pyplot.yticks(range(arr_len),music_genre)
 # 加title,展示图像
 pyplot.title(year)
 pyplot.show()
 
 ...
 ...
 drawbar(a1,b1,1930)

注释1:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
"""
 水平条形图,需要修改以下属性
 orientation="horizontal"
"""
import numpy as np
import matplotlib.pyplot as plt
 
# 数据
n = 5
x = [20, 10, 30, 25, 15]
y = [0,1,2,3,4]
 
# 绘图 x= 起始位置, bottom= 水平条的底部(左侧), y轴, height 水平条的宽度, width 水平条的长度
p1 = plt.bar(x=0, bottom=y, height=0.5, width=x, orientation="horizontal")
pyplot.bar(range(arr_len),singer_num,align='center')
pyplot.bar(x=0, bottom=range(arr_len), height=0.5, width=singer_num, orientation="horizontal")
# 展示图形
plt.show()

python读取excel数据并且画图的实现示例

注释2:plt.xticks的第一个参数和plt.plot的第一个参数一样,第二个参数是和第一个参数相同长度的list此例中用来代替横坐标

?
1
2
3
4
5
6
7
8
9
10
11
12
13
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [1, 4, 9, 6]
labels = ['frogs', 'hogs', 'bogs', 'slogs']
 
plt.plot(x, y)
# you can specify a rotation for the tick labels in degrees or with keywords.
plt.xticks(x, labels, rotation='vertical')
# pad margins so that markers don't get clipped by the axes
plt.margins(0.2)
# tweak spacing to prevent clipping of tick-labels
plt.subplots_adjust(bottom=0.15)
plt.show()

python读取excel数据并且画图的实现示例

1.1 效果:

python读取excel数据并且画图的实现示例

1.2 完整代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import pandas as pd
import numpy as np
import xlrd
from matplotlib import pyplot
def drawbar(music_genre,singer_num,year):
 arr_len=len(music_genre)
 
 i=0
 dict_music_singer={}
 while i<arr_len:
 dict_music_singer[music_genre[i]]=singer_num[i]
 i=i+1
 #pyplot.bar(range(arr_len),singer_num,align='center')
 pyplot.bar(x=0, bottom=range(arr_len), height=0.3, width=singer_num, orientation="horizontal")
 pyplot.yticks(range(arr_len),music_genre)
 pyplot.title(year)
 pyplot.show()
 
 
# 1930
workbook=xlrd.open_workbook('1930.xlsx')
sheet= workbook.sheet_by_index(0)
a1=[]
b1=[]
for i in range(1,sheet.nrows):
 a1.append(sheet.cell_value(i,0))
 b1.append(sheet.cell_value(i,1))
 
if len(a1)!=len(b1):
 print("false")
drawbar(a1,b1,1930)
 
 
 
# 1940
workbook=xlrd.open_workbook('1940.xlsx')
sheet= workbook.sheet_by_index(0)
a2=[]
b2=[]
for i in range(1,sheet.nrows):
 a2.append(sheet.cell_value(i,0))
 b2.append(sheet.cell_value(i,1))
 
if len(a2)!=len(b2):
 print("false")
drawbar(a2,b2,1940)
 
 
 
#
workbook=xlrd.open_workbook('1950.xlsx')
sheet= workbook.sheet_by_index(0)
a3=[]
b3=[]
for i in range(1,sheet.nrows):
 a3.append(sheet.cell_value(i,0))
 b3.append(sheet.cell_value(i,1))
 
if len(a3)!=len(b3):
 print("false")
drawbar(a3,b3,1950)
 
 
 
# 6
workbook=xlrd.open_workbook('1960.xlsx')
sheet= workbook.sheet_by_index(0)
a4=[]
b4=[]
for i in range(1,sheet.nrows):
 a4.append(sheet.cell_value(i,0))
 b4.append(sheet.cell_value(i,1))
 
if len(a4)!=len(b4):
 print("false")
drawbar(a4,b4,1960)
 
 
 
 
#
workbook=xlrd.open_workbook('1970.xlsx')
sheet= workbook.sheet_by_index(0)
a5=[]
b5=[]
for i in range(1,sheet.nrows):
 a5.append(sheet.cell_value(i,0))
 b5.append(sheet.cell_value(i,1))
 
if len(a5)!=len(b5):
 print("false")
drawbar(a5,b5,1970)
 
 
 
 
#
workbook=xlrd.open_workbook('1980.xlsx')
sheet= workbook.sheet_by_index(0)
a6=[]
b6=[]
for i in range(1,sheet.nrows):
 a6.append(sheet.cell_value(i,0))
 b6.append(sheet.cell_value(i,1))
 
if len(a6)!=len(b6):
 print("false")
drawbar(a6,b6,1980)
 
 
 
 
# 9
workbook=xlrd.open_workbook('1990.xlsx')
sheet= workbook.sheet_by_index(0)
a7=[]
b7=[]
for i in range(1,sheet.nrows):
 a7.append(sheet.cell_value(i,0))
 b7.append(sheet.cell_value(i,1))
 
if len(a7)!=len(b7):
 print("false")
drawbar(a7,b7,1990)
 
 
 
 
# 2000
workbook=xlrd.open_workbook('2000.xlsx')
sheet= workbook.sheet_by_index(0)
a8=[]
b8=[]
for i in range(1,sheet.nrows):
 a8.append(sheet.cell_value(i,0))
 b8.append(sheet.cell_value(i,1))
 
if len(a8)!=len(b8):
 print("false")
drawbar(a8,b8,2000)
 
 
 
 
#
workbook=xlrd.open_workbook('2010.xlsx')
sheet= workbook.sheet_by_index(0)
a9=[]
b9=[]
for i in range(1,sheet.nrows):
 a9.append(sheet.cell_value(i,0))
 b9.append(sheet.cell_value(i,1))
 
if len(a9)!=len(b9):
 print("false")
drawbar(a9,b9,2010)
 
 
 
 
# #
# workbook=xlrd.open_workbook('2020.xlsx')
# sheet= workbook.sheet_by_index(0)
# a2=[]
# b2=[]
# for i in range(1,sheet.nrows):
# a2.append(sheet.cell_value(i,0))
# b2.append(sheet.cell_value(i,1))
 
# if len(a2)!=len(b2):
# print("false")
# drawbar(a2,b2,2020)

以上就是python读取excel数据并且画图的实现示例的详细内容,更多关于python读取excel数据并且画图的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/zhengyuhu/p/14387544.html

延伸 · 阅读

精彩推荐