LoginSignup
1
1

More than 1 year has passed since last update.

python グラフ描画の配列引数でのエラー

Last updated at Posted at 2022-11-07

背景

配列の1個目の要素の違いにより、エラーになるケースがあり理由を調査する。
下記のケースで、配列の1個目が文字列の時はエラーが発生する。
エラー

TypeError: 'value' must be an instance of str or bytes, not a float

strでfloatではない気がするがなぜなのか調査する。

うまくいく例 (1)

import matplotlib.pyplot as plt
import numpy as np

test_fig = plt.figure()
ay1 = test_fig.subplots()
list1=[1,2,3,4,5,6]
list2=[np.nan, np.nan, np.nan,"80", "40", np.nan]
ay1.scatter(list1 ,list2, c="orange")
test_fig.savefig("save_filename.png")

うまくいく例 (2)

test_fig = plt.figure()
ay1 = test_fig.subplots()
list1=[1,2,3,4,5,6]
#list2=[np.nan, np.nan, np.nan,"80", "40", np.nan]
list2=[8, np.nan, np.nan, np.nan, 4, np.nan]
ay1.scatter(list1 ,list2, c="orange")
test_fig.savefig("save_filename.png")

エラー例

import matplotlib.pyplot as plt
import numpy as np

test_fig = plt.figure()
ay1 = test_fig.subplots()
list1=[1,2,3,4,5,6]
list2=["80",np.nan, np.nan, np.nan, "40", np.nan]
ay1.scatter(list1 ,list2, c="orange")
test_fig.savefig("save_filename.png")

エラー

TypeError: 'value' must be an instance of str or bytes, not a float

調査中

どうやらnp.nanがfloat型の様で、strが最初で2個目がfloat(nan)だとエラーになる??

import numpy as np

list2=[np.nan, np.nan, np.nan,"80", "40", np.nan]
print(type(list2[0]))
print(type(list2[1]))

list3=["80",np.nan, np.nan, np.nan, "40", np.nan]
print(type(list3[0]))
print(type(list3[1]))
<class 'float'>
<class 'float'>

<class 'str'>
<class 'float'>
1
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1