前提・実現したいこと
Titanicのデータを使って
データ加工の練習をしています。
そこで生存者と死亡者に分け、
それぞれの年齢をbinで区切ったデータを
matplotlibで描画したいと考えています。
発生している問題・エラーメッセージ
ValueError Traceback (most recent call last) <ipython-input-38-71ca1fe4eb72> in <module> 1 plt.figure(figsize = (100, 60)) ----> 2 plt.bar(age, age2, align='center', width = 1.0) 3 plt.xticks(x, ['Surviver', 'Dead']) 4 plt.ylabel('age') 5 plt.grid(True) c:\users\jupyter-notebook\lib\site-packages\matplotlib\pyplot.py in bar(x, height, width, bottom, align, data, **kwargs) 2438 return gca().bar( 2439 x, height, width=width, bottom=bottom, align=align, -> 2440 **({"data": data} if data is not None else {}), **kwargs) 2441 2442 c:\users\jupyter-notebook\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs) 1599 def inner(ax, *args, data=None, **kwargs): 1600 if data is None: -> 1601 return func(ax, *map(sanitize_sequence, args), **kwargs) 1602 1603 bound = new_sig.bind(ax, *args, **kwargs) c:\users\jupyter-notebook\lib\site-packages\matplotlib\axes\_axes.py in bar(self, x, height, width, bottom, align, **kwargs) 2373 x, height, width, y, linewidth = np.broadcast_arrays( 2374 # Make args iterable too. -> 2375 np.atleast_1d(x), height, width, y, linewidth) 2376 2377 # Now that units have been converted, set the tick locations. <__array_function__ internals> in broadcast_arrays(*args, **kwargs) c:\users\jupyter-notebook\lib\site-packages\numpy\lib\stride_tricks.py in broadcast_arrays(*args, **kwargs) 262 args = [np.array(_m, copy=False, subok=subok) for _m in args] 263 --> 264 shape = _broadcast_shape(*args) 265 266 if all(array.shape == shape for array in args): c:\users\jupyter-notebook\lib\site-packages\numpy\lib\stride_tricks.py in _broadcast_shape(*args) 189 # use the old-iterator because np.nditer does not handle size 0 arrays 190 # consistently --> 191 b = np.broadcast(*args[:32]) 192 # unfortunately, it cannot handle 32 or more arguments directly 193 for pos in range(32, len(args), 31): ValueError: shape mismatch: objects cannot be broadcast to a single shape
該当のソースコード
Python
1survivers = data.query("Survived_y == 1") 2dead = data.query("Survived_y == 0") 3bins = [18, 25, 35, 45, 60, 80, 100] 4age = pd.cut(survivers['Age'], bins) 5age2 = pd.cut(dead['Age'], bins) 6 7plt.figure(figsize = (10, 6)) 8plt.bar(age, age2, align='center', width = 0.5) 9plt.xticks(x, ['Surviver', 'Dead']) 10plt.ylabel('age') 11plt.grid(True)
試したこと
エラー的には
plt.bar(age, age2, align='center', width = 1.0)
が違うようなので、この数字をいじってみたりしました。
ただ、調べているとage, age2の形が違い、
互換性がないのが原因のようなのですが、
元々一つのデータだしなーとか思ったりしてます。
ご協力を宜しくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/16 06:12
2019/08/16 06:12
2019/08/16 06:13
2019/08/16 06:15
2019/08/16 06:19
2019/08/16 06:20