PythonでCSVファイルのデータをプロットする

PythonでCSV形式の数値データをmatplotlibを使ってグラフにプロットしてみます。

プロットしたいデータはこんな感じです。

0,4.634364244,12.69486747
0.1,4.898138863,12.20500553
0.2,4.89357395,12.10398765
0.3,5.045166923,12.68143436
0.4,4.63902651,11.73812931
0.5,4.974791614,11.60366345
0.6,5.237071696,10.60787555
0.7,5.18245889,11.05095562
0.8,4.911221111,11.94149701
0.9,5.312648569,11.00267697
1,4.83809443,11.08550192
1.1,5.277243593,10.8479104
1.2,4.99384299,10.69214355
1.3,4.522883777,10.13552688
1.4,4.460771371,10.12715136
1.5,4.193855821,9.588884445
1.6,3.912636859,9.508091377
1.7,3.702418473,8.551070787
1.8,4.039996449,8.663979432
1.9,4.182290812,8.035791964
2,4.674834224,8.755685022
2.1,4.295724184,8.421075393
2.2,4.517208591,8.843458932
2.3,4.953649178,8.687672932
2.4,5.283684872,9.028284065
2.5,5.087053382,9.203445277
2.6,5.469532383,9.895840114
2.7,5.474816204,10.07384463
2.8,5.009342034,9.559324577
2.9,5.306746282,9.387952575
3,4.979753683,9.485550098
3.1,5.182794445,9.834521759
3.2,5.057497466,9.712445019
3.3,5.065923954,10.26933025
3.4,5.086862372,10.05584044
3.5,5.076555892,9.114990367
3.6,4.620043182,9.521754544
3.7,5.1032309,9.708122005
3.8,4.996830586,9.048820399
3.9,4.999069145,10.01297367
4,5.269592284,10.09220857
4.1,5.629882063,9.556560827
4.2,5.643653726,10.4614956
4.3,5.721448534,10.37975907
4.4,5.490728012,10.47575169
4.5,5.947844293,9.487169945
4.6,6.231499526,10.12814177
4.7,6.617679107,10.60914859
4.8,6.926819007,10.64650516
4.9,6.988176872,10.49868652
5,6.54430017,11.23870683
5.1,6.614299504,10.63838567
5.2,6.619019971,10.60823589
5.3,6.475809936,10.30039173
5.4,6.514288731,10.54737064
5.5,6.626741196,10.46366424
5.6,6.15471618,9.922874301
5.7,5.831927439,10.09179604
5.8,6.1929363,10.68867392
5.9,6.490033863,11.32154866
6,6.245327903,12.00503833
6.1,6.418441428,11.1715066
6.2,5.935132058,10.20062655
6.3,6.190718834,9.699745006
6.4,5.800207461,9.949349174
6.5,5.644630325,9.088379931
6.6,5.30425585,9.143140729
6.7,4.972400796,8.688969603
6.8,5.183990723,8.598372863
6.9,5.005992489,8.545914891
7,4.529627067,8.319029101
7.1,4.450545746,7.69510771
7.2,4.059307439,8.494744711
7.3,4.06942342,7.912926696
7.4,4.17507206,8.547006033
7.5,3.695890168,7.582735075
7.6,3.342351909,8.02040602
7.7,3.002579501,8.429617276
7.8,3.180755297,8.519021603
7.9,2.901355045,9.470210639
8,3.199165902,9.503409673
8.1,2.922361682,9.800422509
8.2,2.817259692,9.952114434
8.3,2.638505502,10.21401016
8.4,2.197290618,9.811222056
8.5,2.665193928,10.56229054
8.6,2.471580548,11.27931936
8.7,2.281944176,12.15789622
8.8,2.525786294,11.99024075
8.9,2.278144397,11.00720127
9,2.656862295,10.08303433
9.1,2.976276406,11.00743658
9.2,3.046556976,10.35047077
9.3,3.41433804,11.29802125
9.4,3.618361183,11.31576874
9.5,3.496330017,11.00963051
9.6,3.202091774,11.35793654
9.7,3.135041895,10.74617383
9.8,2.739466118,11.07808888
9.9,2.535538791,11.07768873

X軸の数値、Y軸の数値(系列1)、Y軸の数値(系列2)という感じでデータが並ぶ、よくあるデータです。

特にひねりも無いのですが、forを使って元のデータを列ごとに分離して、pyplotに描画してもらいます。

import csv
import matplotlib.pyplot as plt

fn = 'test.csv'

with open(fn, mode='r', newline='') as f_in:
    reader = csv.reader(f_in)
    data_array = [row for row in reader]

plot_x = []
plot_y1 = []
plot_y2 = []
for i in data_array:
    plot_x.append(i[2])
    plot_y1.append(i[3])
    plot_y2.append(i[4])

plt.plot(plot_x, plot_y1)
plt.plot(plot_x, plot_y2)
plt.show()

表示結果はこうなりました。

../../_images/python_plot_csv_01.png

広告

PythonでCSVカテゴリの投稿