Pythonでグラフを描くために以下のコードを実行すると、ValueError: could not convert string to float: というエラーが出ました。
何方か対処法をお教えしていただけないでしょうか。
よろしくお願いいたします。
<コード>
python
1import numpy as np 2import matplotlib.pyplot as plt 3 4x, y1, y2 = np.loadtxt('/Users/a/Desktop/1-1.csv', delimiter=',',usecols =(0,1,2), unpack=True) 5fig, ax1 = plt.subplots(figsize=(7, 10), facecolor="w") 6ax1.plot(x, y1, 'k') # x, y1 の折れ線グラフをプロットする。 7time_max = x.max(0) 8ax1.set_xticks(np.arange(0,time_max,60)) 9ax1.set_yticks(np.arange(1.0, 2.21, 0.2)) # y 軸の目盛りを設定する。 10ax1.set_yticks(np.arange(0.9, 2.21, 0.2), minor=True) # y 軸の補助目盛りを設定する。 11ax1.set_xlabel('Elapsed time [min]', size = 18) # x 軸のラベルを設定する。 12ax1.set_ylabel('Weight ratio m/m0 [-]', size = 18) # y 軸のラベルを設定する。 13def find_nearest(array, value): 14 idx = (np.abs(array - value)).argmin() 15 return idx 16nearest_idx = [find_nearest(y2, x) for x in [1.1, 1.2, 1.3, 1.4, 1.5]] # 3列目で [1.1, 1.2, 1.3, 1.4, 1.5] に最も近い値のインデックスを求める。 17print(y1[nearest_idx]) 18ax2 = ax1.twinx() 19ax2.set_yticks(y1[nearest_idx]) 20ax2.set_yticklabels([1.1, 1.2, 1.3, 1.4, 1.5]) 21ax2.set_ylabel('Supersaturation S [-]', size = 18) 22ax2.set_ylim(*ax1.get_ylim()) # ax1 と y 軸を同じスケールにする。 23plt.savefig('/Users/a/Desktop/graph.png')
<csvファイル>
csv
11 0.997 2.063009369 22 0.996 2.06446939 33 0.998 2.061551412 44 1.02 2.015960779 55 1.05 1.96835037 66 1.05 1.955157673 77 1.06 1.935696913 88 1.07 1.915361292 99 1.08 1.900387784 1010 1.1 1.873535805 1111 1.11 1.853300666 1212 1.12 1.835805694 1313 1.12 1.834651099 1414 1.14 1.804016851 1515 1.15 1.791827548 1616 1.17 1.761530947 1717 1.18 1.744674191 1818 1.2 1.716948351 1919 1.21 1.697959981 2020 1.23 1.667864636 2121 1.25 1.643433942 2222 1.27 1.618809794 2323 1.28 1.601040202 2424 1.3 1.580224945 2525 1.32 1.557445407 2626 1.34 1.532892931 2727 1.36 1.509883669 2828 1.39 1.484526844 2929 1.4 1.471793768 3030 1.42 1.450569492 3131 1.44 1.428548114 3232 1.47 1.398415747 3333 1.48 1.386452114 3434 1.49 1.379893684 3535 1.55 1.325952385 3636 1.56 1.318759154 3737 1.59 1.290179234 3838 1.62 1.268853957 3939 1.64 1.250898477 4040 1.68 1.223613778 4141 1.7 1.208908101 4242 1.75 1.174826922 4343 1.76 1.166838099 4444 1.78 1.157577479 4545 1.81 1.135498345 4646 1.85 1.11042834 4747 1.88 1.096652349 4848 1.89 1.089281273 4949 1.91 1.076021855 5050 1.92 1.069708562 5151 1.93 1.067750823 5252 1.94 1.060761908 5353 1.95 1.056918568 5454 1.95 1.055389019 5555 1.96 1.049692425 5656 1.96 1.049692425 5757 1.97 1.046679314 5858 1.97 1.045553852 5959 1.97 1.045179236 6060 1.97 1.042937164 6161 1.97 1.042191943 6262 1.97 1.041819731 6363 1.97 1.041819731 6464 1.97 1.041819731 6565 1.97 1.041819731 6666 1.98 1.041447786 6767 1.98 1.041076106 6868 1.98 1.041447786 6969 1.98 1.041076106 7070 1.98 1.041076106 7171 1.97 1.041819731 7272 1.97 1.041819731 7373 1.97 1.042191943 7474 1.97 1.041819731 7575 1.97 1.041819731 7676 1.97 1.042191943 7777 1.97 1.041819731 7878 1.98 1.041447786 7979 1.98 1.041447786 8080 1.98 1.041076106 8181 1.98 1.041447786 8282 1.98 1.040704691 8383 1.99 1.034797889 8484 2 1.028957759 8585 2 1.029684168 8686 2 1.030411603 8787 2 1.030047757 8888 2 1.030047757 8989 2 1.029684168 9090 2 1.029320836 9191 2.01 1.023183181 9292 2.02 1.019963373 9393 2.02 1.019606867 9494 2.02 1.019963373 9595 2.02 1.020320129 9696 2.02 1.019606867 9797 2.01 1.022465912 9898 2.01 1.025341036 9999 2 1.026062345 100100 2 1.026062345 101101 2 1.029320836 102102 1.99 1.032965739 103103 1.99 1.034797889 104104 1.98 1.039221677 105105 1.98 1.041076106 106106 1.96 1.048937522 107107 1.95 1.055770991 108108 1.94 1.059991006 109109 1.93 1.065021996 110110 1.92 1.069708562 111111 1.92 1.073250643 112112 1.91 1.076021855 113113 1.91 1.078408594 114114 1.91 1.078807414 115115 1.9 1.084825306 116116 1.89 1.090095384 117117 1.88 1.09459484 118118 1.87 1.098303934 119119 1.87 1.102454742 120120 1.86 1.104123864 121121 1.85 1.110005802 122122 1.84 1.116805225 123123 1.84 1.12023627 124124 1.83 1.124554837 125125 1.82 1.128470115 126126 1.82 1.130657073 127127 1.82 1.132412752 128128 1.81 1.134615032 129129 1.8 1.140381254 130130 1.79 1.146656937 131131 1.79 1.150274151 132132 1.79 1.150727908 133133 1.78 1.153002074 134134 1.78 1.157118305 135135 1.77 1.160340194 136136 1.77 1.165439572 137137 1.76 1.169176452 138138 1.75 1.175300261 139139 1.75 1.178624343 140140 1.75 1.178624343 141141 1.74 1.18196728 142142 1.73 1.189679954 143143 1.73 1.18871037 144144 1.72 1.193085991 145145 1.72 1.198477916 146146 1.71 1.201439558 147147 1.7 1.207406973 148148 1.7 1.212929417 149149 1.69 1.215963005 150150 1.69 1.219011804 151151 1.69 1.220031471 152152 1.68 1.226701113 153153 1.67 1.232922759 154154 1.67 1.233444079 155155 1.66 1.236057308 156156 1.66 1.240789131 157157 1.65 1.243963858 158158 1.65 1.246089384 159159 1.65 1.249291327 160160 1.64 1.250898477 161161 1.65 1.249826584 162162 1.64 1.252509767 163163 1.64 1.254125214 164164 1.64 1.254664623 165165 1.64 1.252509767 166166 1.64 1.253586269 167167 1.64 1.257368641 168168 1.64 1.257910844 169169 1.64 1.257910844 170170 1.63 1.26226536 171171 1.62 1.267200368 172172 1.63 1.265551084 173173 1.63 1.265002276 174174 1.62 1.267200368 175175 1.62 1.267751086 176176 1.63 1.265551084 177177 1.63 1.265551084 178178 1.62 1.267751086 179179 1.62 1.268302282 180180 1.63 1.265551084 181181 1.62 1.266100368 182182 1.62 1.268302282 183183 1.62 1.266650129 184184 1.63 1.265002276 185185 1.62 1.267200368 186186 1.62 1.269406113 187187 1.62 1.267751086 188188 1.62 1.272174116 189189 1.61 1.274397225 190190 1.62 1.272174116 191191 1.62 1.271065467 192192 1.62 1.27161955 193193 1.61 1.27384072 194194 1.62 1.2732847 195195 1.61 1.274397225 196196 1.61 1.277187061 197197 1.61 1.280551031 198198 1.61 1.281676295 199199 1.61 1.281113416 200200 1.6 1.283932768 201201 1.6 1.284498128 202202 1.61 1.281113416 203203 1.6 1.286197199 204204 1.6 1.289608863 205205 1.6 1.287900772 206206 1.6 1.285630343 207207 1.59 1.29075011 208208 1.59 1.293612083 209209 1.59 1.29075011 210210 1.59 1.291893378 211211 1.59 1.295335368 212212 1.59 1.295335368 213213 1.59 1.294186002 214214 1.58 1.298217734 215215 1.58 1.299953319 216216 1.58 1.298217734 217217 1.59 1.295910816 218218 1.58 1.299374275 219219 1.58 1.302274664 220220 1.58 1.301693551 221221 1.58 1.300532879 222222 1.58 1.302274664 223223 1.58 1.302856297 224224 1.58 1.304604315 225225 1.57 1.308114461 226226 1.57 1.309288711 227227 1.57 1.308114461 228228 1.57 1.308114461 229229 1.57 1.310465071 230230 1.57 1.312824144 231231 1.57 1.312824144 232232 1.57 1.31223358 233233 1.57 1.31223358 234234 1.57 1.312824144 235235 1.56 1.315784956 236236 1.56 1.316973024 237237 1.56 1.318163239 238238 1.56 1.319355607 239239 1.56 1.319355607 240240 1.56 1.319355607 241241 1.56 1.319355607 242242 1.56 1.319355607 243243 1.56 1.320550135 244244 1.55 1.325349954 245245 1.55 1.325952385 246246 1.55 1.325349954 247247 1.55 1.325952385 248248 1.55 1.326555365 249249 1.55 1.326555365 250250 1.55 1.327762971 251251 1.54 1.332006963 252252 1.54 1.332615463 253253 1.54 1.332006963 254254 1.54 1.332615463 255255 1.54 1.333224519 256256 1.54 1.333224519 257257 1.54 1.333834133 258258 1.54 1.333224519 259259 1.54 1.334444304 260260 1.54 1.338731183 261261 1.54 1.338731183 262262 1.54 1.339345844 263263 1.54 1.338731183 264264 1.54 1.338117086 265265 1.54 1.338117086 266266 1.54 1.338731183 267267 1.54 1.339345844 268268 1.53 1.341810142 269269 1.53 1.343664324 270270 1.53 1.344283524 271271 1.53 1.346766042 272272 1.52 1.350507059 273273 1.52 1.350507059 274274 1.52 1.349882114 275275 1.52 1.349257746 276276 1.52 1.349257746 277277 1.52 1.349257746 278278 1.53 1.348633956 279279 1.53 1.348633956 280280 1.52 1.353640486 281281 1.52 1.356788487 282282 1.52 1.357419845 283283 1.51 1.358051791 284284 1.52 1.357419845 285285 1.51 1.358051791 286286 1.51 1.358051791 287287 1.51 1.359951165 288288 1.51 1.35931745 289289 1.51 1.35931745 290290 1.51 1.358684326 291291 1.51 1.358684326 292292 1.51 1.358684326 293293 1.51 1.35931745 294294 1.51 1.361220368 295295 1.51 1.365042231 296296 1.51 1.364403764 297297 1.51 1.364403764 298298 1.51 1.365681296 299299 1.51 1.365042231 300300 1.51 1.365681296 301301 1.5 1.367602085 302302 1.5 1.370815436 303303 1.5 1.370815436 304304 1.5 1.370815436 305305 1.5 1.371459919 306306 1.5 1.371459919 307307 1.5 1.372105008 308308 1.5 1.372750705 309309 1.49 1.377938237 310310 1.49 1.378589437 311311 1.49 1.379241252 312312 1.49 1.379241252 313313 1.49 1.378589437 314314 1.49 1.378589437 315315 1.49 1.379893684 316316 1.49 1.379241252 317317 1.49 1.378589437 318318 1.49 1.379241252 319319 1.49 1.380546734 320320 1.48 1.385793467 321321 1.48 1.386452114 322322 1.48 1.386452114 323323 1.48 1.385793467 324324 1.49 1.385135445 325325 1.49 1.385135445 326326 1.48 1.386452114 327327 1.48 1.385793467 328328 1.48 1.385793467 329329 1.49 1.385135445 330330 1.46 1.409224757 331331 1.41 1.460007632 332332 1.4 1.471793768 333333 1.37 1.49748216 334334 1.35 1.523287336 335335 1.34 1.529677634 336336 1.34 1.539364247 337337 1.3 1.581081435 338338 1.29 1.589697683 339339 1.28 1.604562843 340340 1.27 1.62150931 341341 1.26 1.632398012 342342 1.25 1.64714582 343343 1.24 1.663110176 344344 1.23 1.668818792 345345 1.23 1.673605994 346346 1.22 1.68715746 347347 1.21 1.695985609 348348 1.21 1.697959981 349349 1.21 1.704906632 350350 1.2 1.713922002 351351 1.2 1.718971861 352352 1.2 1.71795951 353353 1.19 1.722016085 354354 1.19 1.733271092 355355 1.18 1.745718281 356356 1.18 1.749907167
<エラー内容>
Traceback (most recent call last):
File "C:\Users\a\Desktop\tips\python\csv\20200102_Master_thesis_excel.py", line 4, in <module>
x, y1, y2 = np.loadtxt('/Users/a/Desktop/2-1.csv', delimiter=',',usecols =(0,1,2), unpack=True)
File "C:\Users\1\AppData\Local\Programs\Python\Python37-32\lib\site-packages\numpy\lib\npyio.py", line 1146, in loadtxt
for x in read_data(_loadtxt_chunksize):
File "C:\Users\a\AppData\Local\Programs\Python\Python37-32\lib\site-packages\numpy\lib\npyio.py", line 1074, in read_data
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "C:\Users\a\AppData\Local\Programs\Python\Python37-32\lib\site-packages\numpy\lib\npyio.py", line 1074, in <listcomp>
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "C:\Users\a\AppData\Local\Programs\Python\Python37-32\lib\site-packages\numpy\lib\npyio.py", line 781, in floatconv
return float(x)
ValueError: could not convert string to float:
回答2件
あなたの回答
tips
プレビュー