Python實(shí)現(xiàn)可視化CSV文件中的數(shù)據(jù)
CSV代表“逗號(hào)分隔值”。這意味著CSV文件中的數(shù)據(jù)(值)由分隔符分隔,即,逗號(hào)CSV文件中的數(shù)據(jù)以擴(kuò)展名為. csv的表格格式存儲(chǔ)。通常,CSV文件與Microsoft Excel工作表一起使用。CSV文件包含許多記錄,數(shù)據(jù)分布在各行和各列中。在本文中,我們將在Python中可視化CSV文件中的數(shù)據(jù)。
要提取CSV文件中的數(shù)據(jù),必須在我們的程序中導(dǎo)入CSV模塊,如下所示:
import csv
with open('file.csv') as File:
Line_reader = csv.reader(File)
例1:可視化條形圖
以下CSV文件包含保存為“biostats. csv”的不同人員姓名、性別和年齡:

import matplotlib.pyplot as plt
import csv
x = []
y = []
with open('biostats.csv','r') as csvfile:
plots = csv.reader(csvfile, delimiter = ',')
for row in plots:
x.append(row[0])
y.append(int(row[2]))
plt.bar(x, y, color = 'g', width = 0.72, label = "Age")
plt.xlabel('Names')
plt.ylabel('Ages')
plt.title('Ages of different persons')
plt.legend()
plt.show()輸出

例2:可視化折線圖

import matplotlib.pyplot as plt
import csv
x = []
y = []
with open('Weatherdata.csv','r') as csvfile:
lines = csv.reader(csvfile, delimiter=',')
for row in lines:
x.append(row[0])
y.append(int(row[1]))
plt.plot(x, y, color = 'g', linestyle = 'dashed',
marker = 'o',label = "Weather Data")
plt.xticks(rotation = 25)
plt.xlabel('Dates')
plt.ylabel('Temperature(°C)')
plt.title('Weather Report', fontsize = 20)
plt.grid()
plt.legend()
plt.show()輸出

例3:可視化散點(diǎn)圖

import matplotlib.pyplot as plt
import csv
Names = []
Values = []
with open('bldprs_measure.csv','r') as csvfile:
lines = csv.reader(csvfile, delimiter=',')
for row in lines:
Names.append(row[0])
Values.append(int(row[1]))
plt.scatter(Names, Values, color = 'g',s = 100)
plt.xticks(rotation = 25)
plt.xlabel('Names')
plt.ylabel('Values')
plt.title('Patients Blood Pressure Report', fontsize = 20)
plt.show()
輸出

例4:可視化餅圖

import matplotlib.pyplot as plt
import csv
Subjects = []
Scores = []
with open('SubjectMarks.csv', 'r') as csvfile:
lines = csv.reader(csvfile, delimiter = ',')
for row in lines:
Subjects.append(row[0])
Scores.append(int(row[1]))
plt.pie(Scores,labels = Subjects,autopct = '%.2f%%')
plt.title('Marks of a Student', fontsize = 20)
plt.show()輸出

到此這篇關(guān)于Python實(shí)現(xiàn)可視化CSV文件中的數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Python可視化CSV數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中關(guān)于列表的常規(guī)操作范例以及介紹
列表是一種有序的集合,可以隨時(shí)添加和刪除其中的元素。在python中使用的頻率非常高,本篇文章對(duì)大家的學(xué)習(xí)或工作具有一定的價(jià)值,需要的朋友可以參考下2021-09-09
Python使用smtplib庫(kù)發(fā)送電子郵件
Python提供了smtplib庫(kù),用于發(fā)送電子郵件,本文將詳細(xì)介紹如何使用Python的smtplib庫(kù)來(lái)發(fā)送電子郵件,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11
通過(guò)conda把已有虛擬環(huán)境的python版本進(jìn)行降級(jí)操作指南
當(dāng)使用conda創(chuàng)建虛擬環(huán)境時(shí),有時(shí)候可能會(huì)遇到python版本不對(duì)的問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于如何通過(guò)conda把已有虛擬環(huán)境的python版本進(jìn)行降級(jí)操作的相關(guān)資料,需要的朋友可以參考下2024-05-05
python+PyQT實(shí)現(xiàn)系統(tǒng)桌面時(shí)鐘
這篇文章主要為大家詳細(xì)介紹了python+PyQT實(shí)現(xiàn)系統(tǒng)桌面時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
pytorch自動(dòng)求梯度autograd的實(shí)現(xiàn)
autograd是一個(gè)自動(dòng)微分引擎,它可以自動(dòng)計(jì)算張量的梯度,本文主要介紹了pytorch自動(dòng)求梯度autograd的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2025-04-04
Python psutil模塊簡(jiǎn)單使用實(shí)例
這篇文章主要介紹了Python psutil模塊簡(jiǎn)單使用實(shí)例,本文直接給出使用腳本,實(shí)現(xiàn)查看cpu的信息、查看內(nèi)存信息、查看系統(tǒng)啟動(dòng)時(shí)間、查看網(wǎng)卡信息等,需要的朋友可以參考下2015-04-04
Python學(xué)習(xí)_幾種存取xls/xlsx文件的方法總結(jié)
今天小編就為大家分享一篇Python學(xué)習(xí)_幾種存取xls/xlsx文件的方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
python opencv實(shí)現(xiàn)圖像邊緣檢測(cè)
這篇文章主要為大家詳細(xì)介紹了python opencv實(shí)現(xiàn)圖像邊緣檢測(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04

