0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CSVファイル(2列)をpython matplotlibにて折れ線/散布図を描く

Last updated at Posted at 2020-11-10

このようなcsvファイルをpython (matplotlib)でプロットしたいとする。。


$ head tmp2
1000,0.059820690
2000,0.093225007
3000,0.139737644
4000,0.185947643
5000,0.249426903
6000,0.280377022
7000,0.325663341
8000,0.374918515
9000,0.421537361
10000,0.467402504

■ scatter(散布図)の場合は、for文で一つ一つプロットしていく。
■ plot(折れ線)の場合は、リストで一気に渡す。

というイメージである。

コードを見てみる。。。

折れ線の場合(14-16行目に注目)


     1import numpy as np
     2import matplotlib.pyplot as plt
     3
     4data_set = np.loadtxt(
     5    fname="tmp2",
     6    dtype="float",
     7    delimiter=",",
     8)
     9
    10x = []
    11y = []
    12
    13for data in data_set:
    14    #plt.scatter(data[0], data[1], c='black')                                                                                                                                         
    15    x.append(data[0])
    16    y.append(data[1])
    17
    18plt.plot(x, y)
    19
    24plt.show()

plot.png

散布図の場合(14-16行目に注目)


     1import numpy as np
     2import matplotlib.pyplot as plt
     3
     4data_set = np.loadtxt(
     5    fname="tmp2",
     6    dtype="float",
     7    delimiter=",",
     8)
     9
    10x = []
    11y = []
    12
    13for data in data_set:
    14    plt.scatter(data[0], data[1], c='black')                                                                                                                                         
    15    #x.append(data[0])
    16    #y.append(data[1])
    17
    18plt.plot(x, y)
    19
    24plt.show()

scatter.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?