matplotlibでプロットごとに色の違うエラーバー付きのグラフを作りたいと思い、下のようなコードを書いたのですがエラーになってしまいます。
python
1def plot_df(file_path, df): 2 df['Color'] = "darkorchid" #新たにColor列を追加 3 for i in range(len(df)): 4 if df.iat[i,7]<0.55 and df.iat[i,3]>3.0: 5 df.iat[i,12] = "red" 6 elif df.iat[i,7]>0.55 and df.iat[i,3]<2.0: 7 df.iat[i,12] = "deepskyblue" 8 elif df.iat[i,7]>0.55 and df.iat[i,3]>2.0: 9 df.iat[i,12] = 'orange' 10 ax1.errorbar(df.A, df.D, yerr=[df.D-df.E,df.F-df.D],fmt='o', elinewidth=2, color=df.Color) 11 print(df) 12 fig.savefig(file_path) 13 plt.close() 14 15plot_df( args.o + "/DF.svg", df)
output
1 A B C D E F G H I J K L Color 20 0.75 0 661 1.986633 1.899432 2.051757 681 0.495433 0.000021 0.490385 0.500481 TRUE darkorchid 31 1.25 1 527 1.980621 1.910314 2.060078 666 0.505529 0.000012 0.500481 0.505529 TRUE darkorchid 42 9.75 0 101 2.04475 1.854302 2.323155 94 0.490385 0.000075 0.480288 0.505529 TRUE darkorchid 53 10.25 1 313 2.086478 1.995747 2.178317 328 0.490385 0.000021 0.485337 0.495433 TRUE darkorchid 64 10.75 0 217 1.793147 1.671491 1.933901 201 0.495433 0.000026 0.485337 0.500481 TRUE darkorchid 75 11.25 1 450 2.070141 1.917563 2.129933 321 0.490385 0.000034 0.485337 0.495433 TRUE darkorchid 86 11.75 0 160 2.093395 1.943261 2.22123 270 0.490385 0.000013 0.485337 0.495433 TRUE darkorchid 97 12.25 1 467 1.984638 1.891135 2.061137 436 0.495433 0.000015 0.490385 0.500481 TRUE darkorchid 108 13.25 1 199 1.417219 1.324536 1.508012 48 0.924519 0.00221 0.909375 0.934615 FALSE deepskyblue 119 14.25 1 384 1.136011 1.068218 1.173075 157 0.909375 0.001709 0.899279 0.919471 FALSE deepskyblue 1210 15.25 1 361 1.177558 1.107492 1.226512 84 0.899279 0.000081 0.884135 0.909375 FALSE deepskyblue 1311 15.75 0 286 0.910397 0.876352 0.955109 77 0.914423 0.000053 0.904327 0.919471 FALSE deepskyblue 1412 16.25 1 238 1.002827 0.947357 1.086941 38 0.94976 0.00002 0.944712 0.954808 FALSE deepskyblue 1513 16.75 0 220 0.898391 0.860192 0.965455 35 0.919471 0.000096 0.904327 0.929567 FALSE deepskyblue 1614 17.25 1 523 0.998418 0.945097 1.04597 145 0.914423 0.003539 0.904327 0.919471 FALSE deepskyblue 1715 17.75 0 45 1.832847 1.69962 2.13632 49 0.485337 0.000558 0.465144 0.525721 FALSE darkorchid 1816 18.25 1 107 2.419603 2.068424 2.559209 105 0.495433 0.000047 0.485337 0.505529 TRUE darkorchid 1917 18.75 0 408 1.34888 1.295705 1.40245 498 0.485337 0.000011 0.480288 0.485337 FALSE darkorchid 2018 19.25 1 504 1.575057 1.48671 1.624477 350 0.495433 0.000029 0.485337 0.500481 TRUE darkorchid 2119 1.75 0 341 1.717256 1.632602 1.820001 266 0.545913 0.000581 0.500481 0.566106 FALSE darkorchid 2220 2.25 1 410 1.667634 1.605959 1.717117 261 0.596394 0.000305 0.58125 0.60649 FALSE deepskyblue 2321 19.75 0 101 1.864069 1.624894 2.094754 140 0.47524 0.00009 0.470192 0.485337 FALSE darkorchid 2422 20.25 1 220 1.625464 1.513253 1.774683 239 0.500481 0.000068 0.490385 0.510577 TRUE darkorchid 2523 21.25 1 108 2.022508 1.915893 2.315803 178 0.495433 0.000058 0.485337 0.505529 TRUE darkorchid 2624 22.25 1 304 1.67197 1.589657 1.750276 334 0.485337 0.000045 0.480288 0.495433 FALSE darkorchid 2725 2.75 0 339 1.05149 1.006327 1.100201 85 0.924519 0.000443 0.914423 0.929567 FALSE deepskyblue 2826 3.25 1 316 1.239 1.17295 1.28326 75 0.924519 0.00003 0.919471 0.929567 FALSE deepskyblue 2927 3.75 0 140 2.1016 1.968814 2.312215 145 0.495433 0.00006 0.485337 0.505529 TRUE darkorchid 3028 4.25 1 249 2.202444 2.054302 2.331457 238 0.485337 0.000037 0.47524 0.490385 FALSE darkorchid 3129 4.75 0 74 2.543123 2.196363 2.756209 78 0.500481 0.000059 0.490385 0.510577 TRUE darkorchid 3230 5.25 1 421 2.017389 1.947913 2.192126 402 0.485337 0.000023 0.480288 0.490385 FALSE darkorchid 3331 5.75 0 348 1.897056 1.788694 1.964566 719 0.495433 0.000011 0.490385 0.500481 TRUE darkorchid 3432 6.25 1 238 2.270184 2.188647 2.410803 325 0.495433 0.000055 0.485337 0.505529 TRUE darkorchid 3533 6.75 0 173 1.141547 1.054863 1.256775 39 0.914423 0.000126 0.894231 0.924519 FALSE deepskyblue 3634 7.25 1 353 1.029177 0.951397 1.13009 89 0.879087 0.014417 0.535817 0.899279 FALSE deepskyblue 3735 7.75 0 133 1.1682 1.069749 1.273256 26 0.848798 0.008891 0.662019 0.889183 FALSE deepskyblue 3836 8.25 1 233 1.149832 1.068551 1.205658 48 0.919471 0.000043 0.909375 0.924519 FALSE deepskyblue 3937 8.75 0 109 1.20045 1.015858 1.299144 12 0.919471 0.009825 0.692308 0.939663 FALSE deepskyblue 4038 9.25 1 338 1.019992 0.972114 1.064449 46 0.899279 0.000895 0.889183 0.914423 FALSE deepskyblue 4139 22.75 0 153 1.58128 1.491089 1.799443 58 0.500481 0.001551 0.47524 0.571154 FALSE darkorchid 4240 23.25 1 225 1.852923 1.691953 2.029317 22 0.773077 0.010297 0.515625 0.813462 FALSE deepskyblue 43Traceback (most recent call last): 44 File "test_df.py", line 245, in <module> 45 plot_copy_rate( args.o + "/DF.svg", df) 46 File "test_df.py", line 197, in plot_df 47 fig.savefig(file_path) 48 File "/home/hoge/anaconda3/lib/python3.7/site-packages/matplotlib/figure.py", line 2311, in savefig 49 self.canvas.print_figure(fname, **kwargs) 50 File "/home/hoge/anaconda3/lib/python3.7/site-packages/matplotlib/backend_bases.py", line 2217, in print_figure 51 **kwargs) 52 File "/home/hoge/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_svg.py", line 1329, in print_svg 53 self._print_svg(filename, fh, **kwargs) 54 File "/home/hoge/anaconda3/lib/python3.7/site-packages/matplotlib/backend_bases.py", line 1639, in wrapper 55 return func(*args, **kwargs) 56 File "/home/hoge/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_svg.py", line 1353, in _print_svg 57 self.figure.draw(renderer) 58 File "/home/hoge/anaconda3/lib/python3.7/site-packages/matplotlib/artist.py", line 41, in draw_wrapper 59 return draw(artist, renderer, *args, **kwargs) 60 File "/home/hoge/anaconda3/lib/python3.7/site-packages/matplotlib/figure.py", line 1864, in draw 61 renderer, self, artists, self.suppressComposite) 62 File "/home/hoge/anaconda3/lib/python3.7/site-packages/matplotlib/image.py", line 131, in _draw_list_compositing_images 63 a.draw(renderer) 64 File "/home/hoge/anaconda3/lib/python3.7/site-packages/matplotlib/artist.py", line 41, in draw_wrapper 65 return draw(artist, renderer, *args, **kwargs) 66 File "/home/hoge/anaconda3/lib/python3.7/site-packages/matplotlib/cbook/deprecation.py", line 411, in wrapper 67 return func(*inner_args, **inner_kwargs) 68 File "/home/hoge/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 2747, in draw 69 mimage._draw_list_compositing_images(renderer, self, artists) 70 File "/home/hoge/anaconda3/lib/python3.7/site-packages/matplotlib/image.py", line 131, in _draw_list_compositing_images 71 a.draw(renderer) 72 File "/home/hoge/anaconda3/lib/python3.7/site-packages/matplotlib/artist.py", line 41, in draw_wrapper 73 return draw(artist, renderer, *args, **kwargs) 74 File "/home/hoge/anaconda3/lib/python3.7/site-packages/matplotlib/lines.py", line 797, in draw 75 self.get_markeredgecolor(), self._alpha) 76 File "/home/hoge/anaconda3/lib/python3.7/site-packages/matplotlib/colors.py", line 189, in to_rgba 77 rgba = _to_rgba_no_colorcycle(c, alpha) 78 File "/home/hoge/anaconda3/lib/python3.7/site-packages/matplotlib/colors.py", line 265, in _to_rgba_no_colorcycle 79 raise ValueError("RGBA sequence should have length 3 or 4") 80ValueError: RGBA sequence should have length 3 or 4
一応、作成されていたDF.svgを見ると、枠も罫線もない(目盛りとラベルはある)グラフエリアにプロットはなく指定した通りの色のエラーバーだけが描画されている状態でした。
追記
コードの2行目から9行目を削除しax1.errorbar(df.A, df.D, yerr=[df.D-df.E,df.F-df.D],fmt='o', elinewidth=2, color="darkorchid")
とした場合、正常に終了することは確認しています。
なのでcolor=の指定に問題があるような気もしますが、DF.svgを見ると色指定には一応成功していますし、色の指定にRGBAも使っていませんのでどう対応すれば良いか分かりません。
回答1件
あなたの回答
tips
プレビュー