matplotlib python3

matplotlib でcsv ファイルを読み込むとき、最初の行をスキップする方法

投稿日:

numpy の loadtxt で読み込む行を省略したい場合

スキップしたい行を # 等の記号でコメントアウトして、

loadtxt の comments = ‘#’ とすると省略できる。

import numpy as np
from matplotlib import pyplot as plt

data_set = np.loadtxt(fname='data.csv',dtype='float',delimiter=',',  comments='#')

fig, ax = plt.subplots()

size = 300
for data in data_set:
    ax.scatter(data[0],data[1],size,'blue')

plt.show()

data.csv

# first second
1,3
2,7
3,9
# 4,7
5,1

結果

-matplotlib, python3
-,

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

python3 で、フォルダがなければ作成するプログラム

os.mkdir() 関数を使う。 すでにフォルダが存在している場合は、エラーを表示する。 例 import os folder_name = ‘./python_create_folder’ try …

no image

python3 で、csv ファイルを読み込んで条件をみたす行の内容を表示する方法

csv.reader で読み込み、各行を読み込んで判定する。 例 import csv with open(‘data.csv’, ‘r’) as csv_file: reader = csv.rea …

no image

python3 でタプルを指定した要素で順に並び替える

タプルを、指定した要素で並び替えることができる。「key=lambda x 」で、引数 x を指定。lambda は無名関数を表す。 例 record = [(‘織田信長’, 1534, 15 …

no image

python3 で文字列を入力させて受け取る方法

C言語で scanf のようなものを作るには、python では input() で実現できる。 例 print(‘xの値を入力してください’) x = input() print(‘xの値は ‘ + …

no image

python3 で、dictionary のキー一覧を取得する方法

dict.keys() でキーを取得することができる。 参考:dictionary の値一覧を取得するときは、dict.values() を使う。 例 print(dict.keys())