Python讀取多列數(shù)據(jù)以及用matplotlib制作圖表方法實例
多列數(shù)據(jù)的讀入以及處理
這次我們用到的數(shù)據(jù)是煤炭5500周價格的最高價和最低價。左側(cè)為價格的數(shù)據(jù)表格,右側(cè)為日期。
一、導(dǎo)入數(shù)據(jù)
這里我們就直接跳過講解,如有不懂的,詳見上一篇博客。見代碼。
import matplotlib.pyplot as plt import re plt.rcParams['font.sans-serif'] = ['SimHei'] # 設(shè)置字體 plt.rcParams['axes.unicode_minus'] = False # 設(shè)置正負號 # 導(dǎo)入數(shù)據(jù),日期 with open('日期.csv', encoding='gbk') as oo: day = oo.read() day_str = day.replace('\n', ',') # 換行替換成逗號 day_list = re.split('[,]', day_str) list_days = [] for s in range(len(day_list)-1): # 獲得時間 list_days.append(day_list[s]) # 將x轉(zhuǎn)換成時間類型 # 導(dǎo)入數(shù)據(jù),金額 with open('煤炭5500周價格波動數(shù)據(jù).csv', encoding='gbk') as pp: sk = pp.read() ll = sk.replace('\n', ',') # 換行替換成逗號 list_1 = re.split('[,]', ll) # 分割數(shù)據(jù) list_2 = [] for s in range(len(list_1)-1): list_2.append(int(float(list_1[s])))
現(xiàn)在我們已經(jīng)講數(shù)據(jù)讀取到相關(guān)的列表里,輸出一下。
輸出結(jié)果:
['2019/12/27', '2019/12/20', '2019/12/13', '2019/12/6', '2019/11/29', '2019/11/22', '2019/11/15', '2019/11/8', '2019/11/1', '2019/10/25', '2019/10/18', '2019/10/11', '2019/9/27', '2019/9/20', '2019/9/12', '2019/9/12', '2019/9/6', '2019/8/30', '2019/8/23', '2019/8/16', '2019/8/9', '2019/8/2', '2019/7/26', '2019/7/19', '2019/7/12', '2019/7/5', '2019/6/28', '2019/6/21', '2019/6/14', '2019/6/7', '2019/5/31', '2019/5/24', '2019/5/17', '2019/5/10', '2019/4/26', '2019/4/19', '2019/4/12', '2019/4/5', '2019/3/29', '2019/3/22', '2019/3/15', '2019/3/8', '2019/3/1', '2019/2/22', '2019/2/15', '2019/2/1', '2019/1/25', '2019/1/18', '2019/1/18', '2019/1/11', '2019/1/4', '2018/12/28']
[550, 555, 550, 555, 550, 555, 550, 555, 550, 555, 550, 555, 550, 555, 550, 555, 560, 565, 570, 575, 575, 580, 580, 585, 585, 590, 585, 590, 585, 590, 585, 590, 580, 585, 580, 585, 580, 590, 575, 585, 580, 590, 595, 600, 590, 600, 590, 595, 600, 605, 605, 615, 600, 610, 590, 600, 590, 600, 590, 600, 595, 600, 610, 620, 615, 620, 615, 620, 615, 625, 620, 625, 630, 640, 620, 630, 620, 625, 620, 630, 625, 630, 635, 645, 615, 625, 600, 605, 600, 605, 585, 590, 590, 595, 590, 595, 590, 595, 580, 590, 585, 595, 575, 580]
二、處理價格數(shù)據(jù)
我們可以看到0,2,4,6,8.......等偶數(shù)位的數(shù)值是周最低價,而單數(shù)位的數(shù)值是周最高價。我們可以用循環(huán)的方式讀取到相關(guān)的數(shù)據(jù)。
代碼如下。
這樣就可以把數(shù)據(jù)進行分組了。以此類推,可以導(dǎo)入多列數(shù)據(jù)。
根據(jù)觀察可以看到,時間列表是以降序的方式排列的,我們需要將數(shù)據(jù)轉(zhuǎn)置過來,讓列表數(shù)據(jù)改為升序。方法一、調(diào)整導(dǎo)入的CSV文件的數(shù)據(jù)順序。方法二、我們引入reversed()函數(shù)。該函數(shù)有兩種寫法,作用主要是將列表,range(),字典里的數(shù)據(jù)進行逆向排列。
逆轉(zhuǎn)對象:list_x 寫法一、 xxx = reversed(list_x) 寫法二、 直接使用 list(reversed(list_x))
aaa = reversed(list_average) 轉(zhuǎn)置一個作為樣例 # 以上分割取得list_high,low,average # 設(shè)置x軸,y軸標(biāo)簽,設(shè)置表格標(biāo)題 plt.xlabel('時間') plt.ylabel('價格') plt.title('最高價/最低價/均價周期波動圖') plt.legend(loc='upper right') plt.figure(figsize=(9, 8))輸出圖片大小900px*800px
圖表制作
需要的數(shù)據(jù)我們已經(jīng)處理好了,接著就是生成圖表。
import matplotlib.pyplot as plt import re plt.rcParams['font.sans-serif'] = ['SimHei'] # 設(shè)置字體 plt.rcParams['axes.unicode_minus'] = False # 設(shè)置正負號 # 導(dǎo)入數(shù)據(jù),日期 with open('日期.csv', encoding='gbk') as oo: day = oo.read() day_str = day.replace('\n', ',') # 換行替換成逗號 day_list = re.split('[,]', day_str) list_days = [] for s in range(len(day_list)-1): # 獲得時間 list_days.append(day_list[s]) print(list_days) # 將x轉(zhuǎn)換成時間類型 # 導(dǎo)入數(shù)據(jù),金額 with open('煤炭5500周價格波動數(shù)據(jù).csv', encoding='gbk') as pp: sk = pp.read() ll = sk.replace('\n', ',') # 換行替換成逗號 list_1 = re.split('[,]', ll) # 分割數(shù)據(jù) list_2 = [] for s in range(len(list_1)-1): list_2.append(int(float(list_1[s]))) print(list_2) list_high = [] # 最高 list_low = [] # 最低 list_average = [] # 均值 for k in range(len(list_2)): if k % 2 == 0: list_low.append(list_2[k]) list_average.append((list_2[k]+list_2[k+1])/2) else: list_high.append(list_2[k]) aaa = reversed(list_average) # 以上分割取得list_high,low,average # 設(shè)置x軸,y軸標(biāo)簽,設(shè)置表格標(biāo)題 plt.xlabel('時間') plt.ylabel('價格') plt.title('最高價/最低價/均價周期波動圖') # 設(shè)置標(biāo)注 plt.figure(figsize=(9, 8)) # 制作折現(xiàn)圖 plt.plot(range(len(list_low)), list(reversed(list_high)), label='最高價', color='brown',marker='o',markerfacecolor='c',markersize='5') plt.plot(range(len(list_low)), list(reversed(list_low)), label='最低價', color='skyblue',marker='s',markerfacecolor='r',markersize='5') plt.plot(range(len(list_low)), list(reversed(list_average)), label='均價', color='lawngreen',marker='h',markerfacecolor='coral',markersize='5') # 設(shè)置標(biāo)注 plt.legend(loc='upper right') # 右上upper right 右下lower right plt.show()
這是到目前我們制作出來的折線圖
替換x軸坐標(biāo)點更改成日期
這里我們使用到plt.xticks()
書寫格式: plt.xticks(被替換的數(shù)值(數(shù)據(jù)長的的列表),替換的數(shù)據(jù),數(shù)據(jù)方向(默認橫向)) plt.xticks(range(len(list_low)), list(reversed(list_days)), rotation='vertical') vertical:數(shù)值方向,也可以寫角度。
到這了我們就完成了全部的代碼。
結(jié)束:最終代碼
import matplotlib.pyplot as plt import re plt.rcParams['font.sans-serif'] = ['SimHei'] # 設(shè)置字體 plt.rcParams['axes.unicode_minus'] = False # 設(shè)置正負號 # 導(dǎo)入數(shù)據(jù),日期 with open('日期.csv', encoding='gbk') as oo: day = oo.read() day_str = day.replace('\n', ',') # 換行替換成逗號 day_list = re.split('[,]', day_str) list_days = [] for s in range(len(day_list)-1): # 獲得時間 list_days.append(day_list[s]) print(list_days) # 將x轉(zhuǎn)換成時間類型 # 導(dǎo)入數(shù)據(jù),金額 with open('煤炭5500周價格波動數(shù)據(jù).csv', encoding='gbk') as pp: sk = pp.read() ll = sk.replace('\n', ',') # 換行替換成逗號 list_1 = re.split('[,]', ll) # 分割數(shù)據(jù) list_2 = [] for s in range(len(list_1)-1): list_2.append(int(float(list_1[s]))) print(list_2) list_high = [] # 最高 list_low = [] # 最低 list_average = [] # 均值 for k in range(len(list_2)): if k % 2 == 0: list_low.append(list_2[k]) list_average.append((list_2[k]+list_2[k+1])/2) else: list_high.append(list_2[k]) aaa = reversed(list_average) # 以上分割取得list_high,low,average # 設(shè)置x軸,y軸標(biāo)簽,設(shè)置表格標(biāo)題 plt.xlabel('時間') plt.ylabel('價格') plt.title('最高價/最低價/均價周期波動圖') # 設(shè)置標(biāo)注 plt.figure(figsize=(9, 8)) plt.xticks(range(len(list_low)), list(reversed(list_days)), rotation='vertical') # 設(shè)置折現(xiàn)圖 plt.plot(range(len(list_low)), list(reversed(list_high)), label='最高價', color='brown',marker='o',markerfacecolor='c',markersize='5') plt.plot(range(len(list_low)), list(reversed(list_low)), label='最低價', color='skyblue',marker='s',markerfacecolor='r',markersize='5') plt.plot(range(len(list_low)), list(reversed(list_average)), label='均價', color='lawngreen',marker='h',markerfacecolor='coral',markersize='5') # 設(shè)置標(biāo)注 plt.legend(loc='upper right') plt.show()
結(jié)果示意圖:
總結(jié)
到此這篇關(guān)于Python讀取多列數(shù)據(jù)以及用matplotlib制作圖片的文章就介紹到這了,更多相關(guān)Python讀取多列數(shù)據(jù)用matplotlib制作圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python數(shù)據(jù)可視化之使用matplotlib繪制簡單圖表
- Python數(shù)據(jù)可視化之基于pyecharts實現(xiàn)的地理圖表的繪制
- Python讀取Excel數(shù)據(jù)并生成圖表過程解析
- Python數(shù)據(jù)可視化 pyecharts實現(xiàn)各種統(tǒng)計圖表過程詳解
- python實現(xiàn)字符串加密 生成唯一固定長度字符串
- python實現(xiàn)對指定字符串補足固定長度倍數(shù)截斷輸出的方法
- python 按照固定長度分割字符串的方法小結(jié)
- Python?實操顯示數(shù)據(jù)圖表并固定時間長度
相關(guān)文章
詳盡講述用Python的Django框架測試驅(qū)動開發(fā)的教程
這篇文章主要介紹了詳盡講述用Python的Django框架測試驅(qū)動開發(fā)的教程,主要使用TDD工具,全文介紹非常詳細,需要的朋友可以參考下2015-04-04Python?matplotlib實戰(zhàn)之箱型圖繪制
箱型圖(Box?Plot),也稱為盒須圖或盒式圖,是一種用作顯示一組數(shù)據(jù)分布情況的統(tǒng)計圖,因型狀如箱子而得名,本文主要為大家介紹了如何使用Matplotlib繪制箱型圖,需要的小伙伴可以參考下2023-08-08基于Python2、Python3中reload()的不同用法介紹
今天小編就為大家分享一篇基于Python2、Python3中reload()的不同用法介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python實現(xiàn)將數(shù)據(jù)庫一鍵導(dǎo)出為Excel表格的實例
下面小編就為大家?guī)硪黄狿ython實現(xiàn)將數(shù)據(jù)庫一鍵導(dǎo)出為Excel表格的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12Python把excel文件數(shù)據(jù)轉(zhuǎn)化為字典格式存儲詳解
這篇文章主要介紹了Python把excel文件數(shù)據(jù)轉(zhuǎn)化為字典格式存儲詳解,在Python中有時候需要操作excel表格的數(shù)據(jù),把excel表格轉(zhuǎn)化為字典存起來,方便讀取,今天我們就來看看如何轉(zhuǎn)換,需要的朋友可以參考下2023-08-08pandas dataframe拼接后index重新排序方式
這篇文章主要介紹了pandas dataframe拼接后index重新排序方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10