gpt4 book ai didi

python - Seaborn 分布图 : unsupported operand type(s) for/: 'str' and 'int'

转载 作者:行者123 更新时间:2023-12-01 02:17:10 25 4
gpt4 key购买 nike

我已经篡改这个脚本有一段时间了,似乎无法弄清楚我做错了什么。

脚本:

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的建议,我仔细检查了所有数据输入,并最终将所有输入转换为 float 。这解决了问题。

改编后的代码是:

dataset[count]['Oxy'].append(float(i))

这也是针对脱氧输入完成的。

关于python - Seaborn 分布图 : unsupported operand type(s) for/: 'str' and 'int' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48286431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com