は私のような、最初の行とそれぞれの名前以下のデータ全体のサンプル名で構成されたデータが含まれているCSVファイルを読み込み、Pythonスクリプトを作成しようとしています:私は描画しようとしているデータセットからPythonでCSVファイルのデータからいくつかの累積分布関数をプロットする方法は?
sample1,sample2,sample3
343.323,234.123,312.544
同じ軸上の各サンプルの累積分布関数。以下のコードを使用してください:
import matplotlib.pyplot as plt
import numpy as np
import csv
def isfloat(value):
'''make sure sample values are floats
(problem with different number of values per sample)'''
try:
float(value)
return True
except ValueError:
return False
def createCDFs (dataset):
'''create a dictionary with sample name as key and data for each
sample as one list per key'''
dataset = dataset
num_headers = len(list(dataset))
dict_CDF = {}
for a in dataset.keys():
dict_CDF["{}".format(a)]= 1. * np.arange(len(dataset[a]))/(len(dataset[a]) - 1)
return dict_CDF
def getdata():
'''retrieve data from a CSV file - file must have sample names in first row
and data below'''
with open('file.csv') as csvfile:
reader = csv.DictReader(csvfile, delimiter = ',')
#create a dict that has sample names as key and associated ages as lists
dataset = {}
for row in reader:
for column, value in row.iteritems():
if isfloat(value):
dataset.setdefault(column, []).append(value)
else:
break
return dataset
x = getdata()
y = createCDFs(x)
#plot data
for i in x.keys():
ax1 = plt.subplot(1,1,1)
ax1.plot(x[i],y[i],label=str(i))
plt.legend(loc='upper left')
plt.show()
これは、以下の出力を与え、サンプル(図1AのSample1)の1つだけを正しく表示します。
Figure 1A. Only one CDF is displaying correctly (Sample1). B. Expected output
サンプルあたりの値の数は異なり、私は私のどこに問題がある、これがあると思います。
解決策がかなり単純であるべきだと私は考えています。助けや助言が役に立ちます。私は単にデータを正しく表示する方法を知りたいだけです。データはhereです。予想される出力を図1Bに示します。
を? – user2699
Excelで生成された期待出力の画像を追加しました – Ton
複数のリンクがある場合は、以前の画像しか表示されませんか? – user2699