“ matplotlib-bilibili,抖音很火的動態資料視頻自動生成(第四節)-視頻,語音自動合成”

還記得上一節中我們所提到的資料動態視頻嗎??這次,為了讓大家更加方便的制作自動生成視頻,我們可在excel中自由配置資料,方便大家操作?,并且還增加了視頻和語音自動合成的方式?,
01— 優化操作
第一步?:匯入xlrd模塊,并定義excel模板

如上就是我們需要的模板?,
接下來我們來處理資料,

第二步?:處理excel,回傳相同時間的資料?,
import operator
import xlrd
?
f=xlrd.open_workbook("資料.xls")# 打開excel檔案讀取資料
sheet=f.sheet_by_index(0)
?
def sort_sheet(hour,name='數量'):
list=[]
ncols=sheet.ncols
for i in range(1,sheet.nrows):
if str(int(sheet.row_values(i)[1]))==str(hour):
dict = {}
for j in range(ncols):
dict[sheet.cell_value(0,j)]=sheet.row_values(i)[j]
list.append(dict)
return sorted(list,key=operator.itemgetter(name))#升序排列
# return sorted(list, key=operator.itemgetter(name),reverse=True) # 降序排列
第三步?:定義顏色
color={"x":"#adb0ff","y":"#ffb3ff","z":"#90d595","a":"#FDF5E6"}
第四步?:建立圖表
from matplotlib import animation
from matplotlib import pyplot
from pylab import mpl
?
mpl.rcParams['font.sans-serif'] =["SimHei"]
mpl.rcParams['axes.unicode_minus'] = False
fig, ax = pyplot.subplots()#回傳一個包含figure和axes物件的元組,將元組分解為fig和ax兩個變數
?
def graph(num):
ax.clear()#清除,不疊加
list=sort_sheet(num)#呼叫上面定義的函式
for i,chart_barh in enumerate(list):
ax.barh(i,chart_barh["數量"],color=color[chart_barh["顏色"]])# 繪制水平方向的條形圖barh()
ax.text(chart_barh["數量"],i,chart_barh["名稱"],size=14,weight=600,ha='left',va='bottom')#添加文字并設定樣式
ax.text(350, 2, num, size=20, weight=600, ha='left', va='bottom')
ax.xaxis.set_ticks_position('top')
ax.set_axisbelow(True)
pyplot.xlim(0,300)
pyplot.title('不同用戶的文章點贊情況')#添加圖示題
?
animator=animation.FuncAnimation(fig, graph, frames=range(1,24))
pyplot.show()#移動到函式外面,不然不會動態顯示
制作結果?:如圖

第五步?:視頻,音頻合成,這里需要引入?os模塊,
cmd = "ffmpeg -i %s -i %s %s" % (self.mp3, self.mp4, self.new_mp4)os.system(cmd)
注意:需要安裝ffmpeg程式
windows如何安裝ffmpeg
02— 完整代碼
import os
from matplotlib import animation
from matplotlib import pyplot
from pylab import mpl
import operator
import xlrd
?
class chart(object):
def __init__(self):
f = xlrd.open_workbook("資料.xls") # 打開excel檔案讀取資料
self.sheet = f.sheet_by_index(0)
self.color = {"x": "#adb0ff", "y": "#ffb3ff", "z": "#90d595", "a": "#FDF5E6"}
mpl.rcParams['font.sans-serif'] = ["SimHei"]
mpl.rcParams['axes.unicode_minus'] = False
self.fig, self.ax = pyplot.subplots() # 回傳一個包含figure和axes物件的元組,將元組分解為fig和ax兩個變數
self.mp4 = 'E:\\資料動態展示.mp4'#生成視頻名稱,絕對路徑
self.mp3 = 'E:\\5018.mp3'#音頻路徑
self.new_mp4 = 'E:\\new合成.mp4'#合成的mp4路徑
def sort_sheet(self,num,name='數量'):
list=[]
ncols=self.sheet.ncols
for i in range(1,self.sheet.nrows):
if str(int(self.sheet.row_values(i)[1]))==str(num):
dict = {}
for j in range(ncols):
dict[self.sheet.cell_value(0,j)]=self.sheet.row_values(i)[j]
list.append(dict)
return sorted(list,key=operator.itemgetter('數量'))#升序排列
# return sorted(list, key=operator.itemgetter(name),reverse=True) # 降序排列
def graph(self,num):
self.ax.clear()#清除,不疊加
list=self.sort_sheet(num)
for i,chart_barh in enumerate(list):
self.ax.barh(i,chart_barh["數量"],color=self.color[chart_barh["顏色"]])# 繪制水平方向的條形圖barh()
self.ax.text(chart_barh["數量"],i,chart_barh["名稱"],size=14,weight=600,ha='left',va='bottom')#添加文字并設定樣式
self.ax.text(250, 2, str(num)+":00時", size=20, weight=600, ha='left', va='bottom')
self.ax.text(200,0, "<---巍然不動", size=16, weight=600, ha='left', va='bottom')
self.ax.xaxis.set_ticks_position('top')
self.ax.set_axisbelow(True)
pyplot.xlim(0,300)
pyplot.title('不同用戶的文章點贊情況')#添加圖示題
def mp4_and_mp3(self):
animator = animation.FuncAnimation(self.fig, self.graph, frames=range(1, 24))
animator.save(self.mp4)
pyplot.show()
cmd = "ffmpeg -i %s -i %s %s" % (self.mp3, self.mp4, self.new_mp4)
os.system(cmd)
if __name__ == '__main__':
chart=chart()
chart.mp4_and_mp3()

相關推薦(個人主頁也有哦,趕快收藏起來)?:
動態資料視頻(第一節)
bilibili,抖音很火的動態資料視頻自動生成(第二節)
matplotlib-bilibili,抖音很火的動態資料視頻自動生成(第三節)
matplotlib給女朋友畫一個愛心吧,這份滿滿的愛意,一定要記得收下
歡迎關注我們
大家一起學編程
以學習為主,興趣為輔,致力于開發,發現更多好玩有趣的編程技巧以及好玩有趣的編程思路,為喜歡編程和想要學習編程的人找到編程的樂趣和動力,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/279270.html
標籤:python
上一篇:一篇文章讓你快速入門Pandas
