我已经对该脚本进行了一段时间的修改,似乎无法弄清楚我在做什么错。
剧本:
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import glob
# Load data set
dataset = { 1:{"Oxy":[], "De-oxy":[]}, 2:{"Oxy":[], "De-oxy":[]}, 3:{"Oxy":[], "De-oxy":[]}, 4:{"Oxy":[], "De-oxy":[]},
5:{"Oxy":[], "De-oxy":[]}, 6:{"Oxy":[], "De-oxy":[]}, 7:{"Oxy":[], "De-oxy":[]}, 8:{"Oxy":[], "De-oxy":[]}, 9:{"Oxy":[], "De-oxy":[]}, 10:{"Oxy":[], "De-oxy":[]},
11:{"Oxy":[], "De-oxy":[]}, 12:{"Oxy":[], "De-oxy":[]}, 13:{"Oxy":[], "De-oxy":[]}, 14:{"Oxy":[], "De-oxy":[]},
15:{"Oxy":[], "De-oxy":[]}, 16:{"Oxy":[], "De-oxy":[]}, 17:{"Oxy":[], "De-oxy":[]}, 18:{"Oxy":[], "De-oxy":[]}, 19:{"Oxy":[], "De-oxy":[]}, 20:{"Oxy":[], "De-oxy":[]},
21:{"Oxy":[], "De-oxy":[]}, 22:{"Oxy":[], "De-oxy":[]}, 23:{"Oxy":[], "De-oxy":[]}, 24:{"Oxy":[], "De-oxy":[]}, 25:{"Oxy":[], "De-oxy":[]}, 26:{"Oxy":[], "De-oxy":[]}, 27:{"Oxy":[], "De-oxy":[]}, 28:{"Oxy":[], "De-oxy":[]}, 29:{"Oxy":[], "De-oxy":[]}, 30:{"Oxy":[], "De-oxy":[]}}
oxy_files = glob.glob("/Users/gabagaba/Documents/MATLAB/Subject-output/Oxy/*.txt")
deoxy_files = glob.glob("/Users/gabagaba/Documents/MATLAB/Subject-output/DeOxy/*.txt")
for item in sorted(oxy_files): # Put all oxy readings in dataset_oxy
with open(item) as file:
content = ""
for line in file:
content = content + line.replace("\n", ",")
items = content.split(',')
count = 1
for i in items:
if ( count < 31 ):
dataset[count]['Oxy'].append(i)
else:
pass
count += 1
for item in sorted(deoxy_files): # Put all de-oxy readings in dataset_deoxy
with open(item) as file:
content = ""
for line in file:
content = content + line.replace("\n", ",")
items = content.split(',')
count = 1
for i in items:
if ( count < 31 ):
dataset[count]['De-oxy'].append(i)
else:
pass
count += 1
fig = plt.figure(figsize = (15,10))
ax = plt.gca()
j = 1
for a in sorted(dataset.keys()):
val = pd.DataFrame({'Oxy': dataset[a]['Oxy'], 'De-oxy': dataset[a]['De-oxy']})
subax = plt.subplot(5,6,j)
try:
sns.distplot(val, bins=29, hist=True, kde=True, rug=True,
ax = subax, color = 'k', norm_hist = True) # This distplot function generates the error from the dataframe input.
except Exception as Ex:
print Ex
subax.set_title("Sensor " + str(a))
subax.set_xlim(-5,5)
j += 1
plt.subplots_adjust(left = 0.06, right = 0.99, bottom = 0.07,
top = 0.92, wspace = 0.14, hspace = 0.6)
fig.text(0.5, 0.02, 'De-oxy', ha ='center', fontsize = 15)
fig.text(0.02, 0.5, 'Oxy', ha ='center',
rotation='vertical', fontsize = 15)
plt.show()
val数据框如下所示:
De-oxy Oxy
0 0.00006010 0.00004332
1 0.00001508 -0.00039567
2 0.00002082 -0.00018257
3 0.00001455 -0.00014494
4 0.00003703 -0.00007764
5 0.00001842 -0.00030163
6 0.00006357 -0.00024539
7 0.00006357 -0.00024539
8 -0.00000054 -0.00018082
9 0.00001561 0.00000669
10 0.00010251 -0.00017378
11 0.00004183 -0.00022999
12 -0.00001308 0.00000502
13 0.00002442 -0.00007114
14 0.00000000 0.00000000
15 -0.00001547 0.00007255
16 -0.00000693 0.00000565
17 -0.00002355 0.00007807
18 0.00000000 0.00000000
19 -0.00000884 0.00006787
20 0.00001404 -0.00010094
21 0.00009296 -0.00035937
22 0.00000000 0.00000000
23 0.00000825 -0.00006484
24 0.00001220 -0.00012597
25 -0.00002296 0.00006205
26 0.00003026 -0.00012464
27 0.00000340 -0.00002748
28 0.00000149 0.00170630
我对我认为出错的地方发表了一些评论。因此,这里的实际问题是:将数据转换为因标题消息而出错的绘图时,我会丢失什么?
目的是获取数据集中每个键(传感器#)的图,x轴上的含氧量值,y轴上的含氧量。
编辑:
根据要求进行回溯:
unsupported operand type(s) for /: 'str' and 'int'
Traceback (most recent call last):
File "testing.py", line 75, in <module>
ax = subax, norm_hist = True)
File "/Users/gabagaba/anaconda/lib/python2.7/site-packages/seaborn/distributions.py", line 192, in distplot
line, = ax.plot(a.mean(), 0)
File "/Users/gabagaba/anaconda/lib/python2.7/site-packages/numpy/core/_methods.py", line 82, in _mean
ret = ret / rcount
TypeError: unsupported operand type(s) for /: 'str' and 'int'
最佳答案
按照@CristiFati的建议,我仔细检查了所有数据输入,最后将所有输入转换为浮点数。这解决了问题。
修改后的代码为:
dataset[count]['Oxy'].append(float(i))
对于脱氧输入也是如此。
关于python - Seaborn distplot:/的不受支持的操作数类型:'str'和'int',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48286431/