欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python讀取excel數(shù)據(jù)并且畫圖的實現(xiàn)示例

 更新時間:2021年02月08日 11:40:03   作者:TR_Goldfish  
這篇文章主要介紹了python讀取excel數(shù)據(jù)并且畫圖的實現(xiàn)示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下

一,要讀取的數(shù)據(jù)的格式:

二,數(shù)據(jù)讀取部分:

b站視頻參考:https://www.bilibili.com/video/BV14C4y1W7Nj?t=148

# 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)
 

三,畫圖函數(shù)

1. def drawBar(Music_genre,singer_num,year)

參數(shù)介紹

參數(shù)名 參數(shù)含義
Music_genre 音樂流派名稱list
singer_num 音樂流派對應(yīng)音樂家數(shù)量list
year 讀的文件的年份(因為源代碼是從1840到2020的)

def drawBar(Music_genre,singer_num,year):
 arr_len=len(Music_genre)
 # 由循環(huán)得到一個字典,key是音樂流派,value是這個音樂流派對應(yīng)的音樂家的數(shù)量
 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:

"""
 水平條形圖,需要修改以下屬性
 orientation="horizontal"
"""
import numpy as np
import matplotlib.pyplot as plt
 
# 數(shù)據(jù)
N = 5
x = [20, 10, 30, 25, 15]
y = [0,1,2,3,4]
 
# 繪圖 x= 起始位置, bottom= 水平條的底部(左側(cè)), 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()

注釋2:plt.xticks的第一個參數(shù)和plt.plot的第一個參數(shù)一樣,第二個參數(shù)是和第一個參數(shù)相同長度的list此例中用來代替橫坐標(biāo)

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()

1.1 效果:

1.2 完整代碼

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數(shù)據(jù)并且畫圖的實現(xiàn)示例的詳細(xì)內(nèi)容,更多關(guān)于python讀取excel數(shù)據(jù)并且畫圖的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python with statement 進(jìn)行文件操作指南

    python with statement 進(jìn)行文件操作指南

    在Python中,with關(guān)鍵字是一個替你管理實現(xiàn)上下文協(xié)議對象的好東西。例如:file等。在file的結(jié)束,會自動關(guān)閉該文件句柄。而這正是本文所需要的
    2014-08-08
  • Python區(qū)塊鏈范圍結(jié)論及Genesis Block的添加教程

    Python區(qū)塊鏈范圍結(jié)論及Genesis Block的添加教程

    這篇文章主要為大家介紹了Python區(qū)塊鏈范圍結(jié)論及Genesis Block的添加,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Python實現(xiàn)從概率分布中隨機(jī)采樣

    Python實現(xiàn)從概率分布中隨機(jī)采樣

    這篇文章主要介紹了通過幾個機(jī)器學(xué)習(xí)中最常用的概率分布為例,來看看如何從一個概率分布中采樣,文章中的代碼對我們的工作或?qū)W習(xí)具有一定價值,感興趣的朋友可以了解一下
    2021-12-12
  • python函數(shù)也可以是一個對象,可以存放在列表中并調(diào)用方式

    python函數(shù)也可以是一個對象,可以存放在列表中并調(diào)用方式

    這篇文章主要介紹了python函數(shù)也可以是一個對象,可以存放在列表中并調(diào)用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • 詳解opencv?rtsp?硬件解碼

    詳解opencv?rtsp?硬件解碼

    這篇文章主要介紹了opencv rtsp硬件解碼的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • python3如何去除字符串中的特殊字符

    python3如何去除字符串中的特殊字符

    這篇文章主要介紹了python3如何去除字符串中的特殊字符,在平時處理字符串的時候,經(jīng)常會遇到字符串中夾雜著我們不希望看到的特殊字符,那么如何處理這些特殊字符呢,今天就跟著小編來看看吧
    2023-04-04
  • python opencv 實現(xiàn)對圖像邊緣擴(kuò)充

    python opencv 實現(xiàn)對圖像邊緣擴(kuò)充

    今天小編就為大家分享一篇python opencv 實現(xiàn)對圖像邊緣擴(kuò)充,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • 淺析Python 字符編碼與文件處理

    淺析Python 字符編碼與文件處理

    這篇文章主要介紹了Python 字符編碼與文件處理的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-09-09
  • pytorch ImageFolder的覆寫實例

    pytorch ImageFolder的覆寫實例

    今天小編就為大家分享一篇pytorch ImageFolder的覆寫實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • python實現(xiàn)列表的排序方法分享

    python實現(xiàn)列表的排序方法分享

    在本篇文章里小編給大家分享了關(guān)于python實現(xiàn)列表的排序方法以及相關(guān)知識點,有興趣的朋友們可以學(xué)習(xí)下。
    2019-07-07

最新評論