pythonで散布図(濃淡図)を作成していました。
matplotlibを実装中に以下のエラーメッセージが発生しました。
エラーメッセージ
C:\Users\comp1\Desktop\python_lesson>python Tc-Te_0005.py sys:1: DtypeWarning: Columns (2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47) have mixed types.Specify dtype option on import or set low_memory=False. sys:1: DtypeWarning: Columns (2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47) have mixed types.Specify dtype option on import or set low_memory=False. <class 'str'> Traceback (most recent call last): File "C:\Users\comp1\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axis.py", line 1573, in convert_units ret = self.converter.convert(x, self.units, self) File "C:\Users\comp1\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\category.py", line 61, in convert unit.update(values) File "C:\Users\comp1\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\category.py", line 212, in update cbook._check_isinstance((str, bytes), value=val) File "C:\Users\comp1\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\cbook\__init__.py", line 2128, in _check_isinstance type_name(type(v)))) TypeError: 'value' must be an instance of str or bytes, not a float The above exception was the direct cause of the following exception: Traceback (most recent call last): File "Tc-Te_0005.py", line 40, in <module> plt.scatter(data.iloc[8:, 40], data.iloc[8:, 42]) File "C:\Users\comp1\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\pyplot.py", line 2816, in scatter None else {}), **kwargs) File "C:\Users\comp1\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\__init__.py", line 1565, in inner return func(ax, *map(sanitize_sequence, args), **kwargs) File "C:\Users\comp1\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\cbook\deprecation.py", line 358, in wrapper return func(*args, **kwargs) File "C:\Users\comp1\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axes\_axes.py", line 4372, in scatter x = self.convert_xunits(x) File "C:\Users\comp1\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\artist.py", line 178, in convert_xunits return ax.xaxis.convert_units(x) File "C:\Users\comp1\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axis.py", line 1576, in convert_units f'units: {x!r}') from e matplotlib.units.ConversionError: Failed to convert value(s) to axis units: 8 13.32512.34513.89513.32514.37613.81412.49612.6... 9 13.32512.34513.89513.32514.37613.81412.49612.6... 10 13.40712.34513.89513.32514.37613.81412.49612.6... 11 13.40712.34513.89513.32514.37613.81412.49612.6... 12 13.40712.34513.89513.32514.37613.81412.49612.6... ... 20979 NaN 20980 NaN 20981 NaN 20982 NaN 20983 NaN Name: c40, Length: 20976, dtype: object
該当のソースコード
import numpy as np import matplotlib.pyplot as plt import seaborn as sns import pandas as pd import os import glob sns.set() sns.set_style('whitegrid') sns.set_palette('gray') col_names = [ 'c{0:02d}'.format(i) for i in range(49) ] list = glob.glob('C:/Users/comp1/Desktop/python_lesson/A000005_csv_daily/202003/*.csv') count=0 for row in list: file = pd.read_csv(row,encoding="shift_jis", names=col_names) if(count==0): data = file count = 1 else: data = data + file data.describe() x = data.iloc[8:, 40] y = data.iloc[8:, 42] x = str(x) y = str(y) print(type(x)) fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.scatter(x, y, s=10, alpha=0.01) plt.scatter(data.iloc[8:, 40], data.iloc[8:, 42]) plt.ylabel('Tcm-2') plt.xlabel('Tem-2') plt.show()
試したこと
複数のcsvデータを読み込み、それを一つのグラフにする際に
plt.scatter(data.iloc[8:, 40], data.iloc[8:, 42])
の行でエラーが出てしまいます
解答よろしくお願いいたします。
dataの実態がわからないと回答のしようがないので、編集して情報を追加してください。
それとコードやエラーメッセージは<code>ボタンで挿入できるコードブロック内に入れてください。
すいません。
もう一度質問を打ち直します。
仮にコード全文があっても、ファイル読み込みがあるコードの場合、実際のファイル(同じフォーマットのダミーでもいいですが)と日本語の説明がないと見る側からしたら理解不能、と申し上げておきます。そしてコードブロックを使わないとインデントが消えて全然読めません(teratailの仕様上)。
一応聞いておきますが、複数のCSVファイルを読み込んむ際に
data = data + file
としておりますが、各CSVデータの値を「足し合わせた」データをプロットしたいというので間違いありませんか?
一つのグラフ上に、各csvデータの (data.iloc[8:, 40], data.iloc[8:, 42]) のデータをプロット
したいというイメージです。
あとできましたら、
data.describe() の行の次の行あたりで
print(data.head())
print(data.dtypes)
print(data.shape)
を実行して、その(それぞれの)結果を貼っていただけませんでしょうか
C:\Users\comp1\Desktop\python_lesson>python Tc-Te_0005.py
sys:1: DtypeWarning: Columns (2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47) have mixed types.Specify dtype option on import or set low_memory=False.
sys:1: DtypeWarning: Columns (2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47) have mixed types.Specify dtype option on import or set low_memory=False.
<class 'str'>
Traceback (most recent call last):
File "Tc-Te_0005.py", line 45, in <module>
df = pd.DateFrame(np.data.iloc[8:, 40], data.iloc[8:, 42], columns=['Tcm-2','Tem-2'])
File "C:\Users\comp1\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\__init__.py", line 263, in __getattr__
raise AttributeError(f"module 'pandas' has no attribute '{name}'")
AttributeError: module 'pandas' has no attribute 'DateFrame'
C:\Users\comp1\Desktop\python_lesson>python Tc-Te_0005.py
sys:1: DtypeWarning: Columns (2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47) have mixed types.Specify dtype option on import or set low_memory=False.
sys:1: DtypeWarning: Columns (2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47) have mixed types.Specify dtype option on import or set low_memory=False.
c00 c01 ... c47 c48
0 VP0300VP0300VP0300VP0300VP0300VP0300VP0300VP03... DataLogger(PC)Ver.1.64DataLogger(PC)Ver.1.64Da... ... NaN NaN
1 ... ----------------------------------------------... NaN
2 年月日年月日年月日年月日年月日年月日年月日年月日年月日年月日年月日年月日年月日年月日年月日年... 時分秒時分秒時分秒時分秒時分秒時分秒時分秒時分秒時分秒時分秒時分秒時分秒時分秒時分秒時分秒時... ... 総合室外機電流値総合室外機電流値総合室外機電流値総合室外機電流値総合室外機電流値総合室外機電... NaN
3 ... floatfloatfloatfloatfloatfloatfloatfloatfloatf... NaN
4 ... 11111111111111111111111111 NaN
[5 rows x 49 columns]
c00 object
c01 object
c02 object
c03 object
c04 object
c05 object
c06 object
c07 object
c08 object
c09 object
c10 object
c11 object
c12 object
c13 object
c14 object
c15 object
c16 object
c17 object
c18 object
c19 object
c20 object
c21 object
c22 object
c23 object
c24 object
c25 object
c26 object
c27 object
c28 object
c29 object
c30 object
c31 object
c32 object
c33 object
c34 object
c35 object
c36 object
c37 object
c38 object
c39 object
c40 object
c41 object
c42 object
c43 object
c44 object
c45 object
c46 object
c47 object
c48 float64
dtype: object
(20984, 49)
<class 'str'>
Traceback (most recent call last):
File "C:\Users\comp1\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axis.py", line 1573, in convert_units
ret = self.converter.convert(x, self.units, self)
File "C:\Users\comp1\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\category.py", line 61, in convert
unit.update(values)
File "C:\Users\comp1\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\category.py", line 212, in update
cbook._check_isinstance((str, bytes), value=val)
File "C:\Users\comp1\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\cbook\__init__.py", line 2128, in _check_isinstance
type_name(type(v))))
TypeError: 'value' must be an instance of str or bytes, not a float
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "Tc-Te_0005.py", line 43, in <module>
plt.scatter(data.iloc[8:, 40], data.iloc[8:, 42])
File "C:\Users\comp1\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\pyplot.py", line 2816, in scatter
None else {}), **kwargs)
File "C:\Users\comp1\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\__init__.py", line 1565, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "C:\Users\comp1\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\cbook\deprecation.py", line 358, in wrapper
return func(*args, **kwargs)
File "C:\Users\comp1\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axes\_axes.py", line 4372, in scatter
x = self.convert_xunits(x)
File "C:\Users\comp1\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\artist.py", line 178, in convert_xunits
return ax.xaxis.convert_units(x)
File "C:\Users\comp1\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axis.py", line 1576, in convert_units
f'units: {x!r}') from e
matplotlib.units.ConversionError: Failed to convert value(s) to axis units: 8 13.32512.34513.89513.32514.37613.81412.49612.6...
9 13.32512.34513.89513.32514.37613.81412.49612.6...
10 13.40712.34513.89513.32514.37613.81412.49612.6...
11 13.40712.34513.89513.32514.37613.81412.49612.6...
12 13.40712.34513.89513.32514.37613.81412.49612.6...
...
20979 NaN
20980 NaN
20981 NaN
20982 NaN
20983 NaN
Name: c40, Length: 20976, dtype: object
というエラーコードになりました