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

Python讀取多列數(shù)據(jù)以及用matplotlib制作圖表方法實(shí)例

 更新時(shí)間:2020年09月23日 09:18:41   作者:住在河邊的狐貍先生  
這篇文章主要給大家介紹了關(guān)于Python讀取多列數(shù)據(jù)以及用matplotlib制作圖表的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

多列數(shù)據(jù)的讀入以及處理

這次我們用到的數(shù)據(jù)是煤炭5500周價(jià)格的最高價(jià)和最低價(jià)。左側(cè)為價(jià)格的數(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è)置正負(fù)號(hào)
# 導(dǎo)入數(shù)據(jù),日期
with open('日期.csv', encoding='gbk') as oo:
  day = oo.read()
day_str = day.replace('\n', ',') # 換行替換成逗號(hào)
day_list = re.split('[,]', day_str)
list_days = []
for s in range(len(day_list)-1): # 獲得時(shí)間
  list_days.append(day_list[s])
# 將x轉(zhuǎn)換成時(shí)間類型
# 導(dǎo)入數(shù)據(jù),金額
with open('煤炭5500周價(jià)格波動(dòng)數(shù)據(jù).csv', encoding='gbk') as pp:
  sk = pp.read()
ll = sk.replace('\n', ',') # 換行替換成逗號(hào)
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]

二、處理價(jià)格數(shù)據(jù)

我們可以看到0,2,4,6,8.......等偶數(shù)位的數(shù)值是周最低價(jià),而單數(shù)位的數(shù)值是周最高價(jià)。我們可以用循環(huán)的方式讀取到相關(guān)的數(shù)據(jù)。

代碼如下。

這樣就可以把數(shù)據(jù)進(jìn)行分組了。以此類推,可以導(dǎo)入多列數(shù)據(jù)。

根據(jù)觀察可以看到,時(shí)間列表是以降序的方式排列的,我們需要將數(shù)據(jù)轉(zhuǎn)置過來,讓列表數(shù)據(jù)改為升序。方法一、調(diào)整導(dǎo)入的CSV文件的數(shù)據(jù)順序。方法二、我們引入reversed()函數(shù)。該函數(shù)有兩種寫法,作用主要是將列表,range(),字典里的數(shù)據(jù)進(jìn)行逆向排列。

逆轉(zhuǎn)對(duì)象:list_x
寫法一、
xxx = reversed(list_x)
寫法二、
直接使用
list(reversed(list_x))
aaa = reversed(list_average) 轉(zhuǎn)置一個(gè)作為樣例
# 以上分割取得list_high,low,average
# 設(shè)置x軸,y軸標(biāo)簽,設(shè)置表格標(biāo)題
plt.xlabel('時(shí)間')
plt.ylabel('價(jià)格')
plt.title('最高價(jià)/最低價(jià)/均價(jià)周期波動(dòng)圖')
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è)置正負(fù)號(hào)
# 導(dǎo)入數(shù)據(jù),日期
with open('日期.csv', encoding='gbk') as oo:
  day = oo.read()
day_str = day.replace('\n', ',') # 換行替換成逗號(hào)
day_list = re.split('[,]', day_str)
list_days = []
for s in range(len(day_list)-1): # 獲得時(shí)間
  list_days.append(day_list[s])
print(list_days)
# 將x轉(zhuǎn)換成時(shí)間類型
# 導(dǎo)入數(shù)據(jù),金額
with open('煤炭5500周價(jià)格波動(dòng)數(shù)據(jù).csv', encoding='gbk') as pp:
  sk = pp.read()
ll = sk.replace('\n', ',') # 換行替換成逗號(hào)
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('時(shí)間')
plt.ylabel('價(jià)格')
plt.title('最高價(jià)/最低價(jià)/均價(jià)周期波動(dòng)圖')
# 設(shè)置標(biāo)注
 
plt.figure(figsize=(9, 8))
 
# 制作折現(xiàn)圖
plt.plot(range(len(list_low)), list(reversed(list_high)), label='最高價(jià)', color='brown',marker='o',markerfacecolor='c',markersize='5')
plt.plot(range(len(list_low)), list(reversed(list_low)), label='最低價(jià)', color='skyblue',marker='s',markerfacecolor='r',markersize='5')
plt.plot(range(len(list_low)), list(reversed(list_average)), label='均價(jià)', 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)點(diǎn)更改成日期

這里我們使用到plt.xticks()

書寫格式:
plt.xticks(被替換的數(shù)值(數(shù)據(jù)長的的列表),替換的數(shù)據(jù),數(shù)據(jù)方向(默認(rèn)橫向))
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è)置正負(fù)號(hào)
# 導(dǎo)入數(shù)據(jù),日期
with open('日期.csv', encoding='gbk') as oo:
  day = oo.read()
day_str = day.replace('\n', ',') # 換行替換成逗號(hào)
day_list = re.split('[,]', day_str)
list_days = []
for s in range(len(day_list)-1): # 獲得時(shí)間
  list_days.append(day_list[s])
print(list_days)
# 將x轉(zhuǎn)換成時(shí)間類型
# 導(dǎo)入數(shù)據(jù),金額
with open('煤炭5500周價(jià)格波動(dòng)數(shù)據(jù).csv', encoding='gbk') as pp:
  sk = pp.read()
ll = sk.replace('\n', ',') # 換行替換成逗號(hào)
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('時(shí)間')
plt.ylabel('價(jià)格')
plt.title('最高價(jià)/最低價(jià)/均價(jià)周期波動(dòng)圖')
# 設(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='最高價(jià)', color='brown',marker='o',markerfacecolor='c',markersize='5')
plt.plot(range(len(list_low)), list(reversed(list_low)), label='最低價(jià)', color='skyblue',marker='s',markerfacecolor='r',markersize='5')
plt.plot(range(len(list_low)), list(reversed(list_average)), label='均價(jià)', 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論