質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

541閲覧

python グラフ描画できないのを解決したい

shougi

総合スコア15

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2018/11/22 03:33

以下のコードが動かない理由がわかりません.トレースバックについてもよくわかりません.
グラフ描画のところまではできるのですが,グラフ描画ができません.グラフ描画するためには何をしなければいけないのでしょうか.

python

1import pandas as pd 2import numpy as np 3 4L5 = ['W-A0', 'W-B0', 'W-F0', 'W-C1', 'W-E1', 'W-A2', 'W-B2', 'W-D2', 'W-F2', 'W-C3', 'W-E3', 5 'W-A4', 'W-B4', 'W-F4'] 6R5 = [r.replace('W', 'R') for r in L5] 7# データリスト 8Ds1 = pd.DataFrame(np.arange(280).reshape(10, 28), columns = L5 + R5) 9Ds2 = pd.DataFrame(np.random.randn(10, 28), columns = L5 + R5) 10 11# リスト 12bL = [900, 700, 500, 300, 100] 13bL1 = [1100, 1300, 1500, 1700, 1900] 14hL = [900, 700, 100, 550, 250, 900, 700, 400, 100, 550, 250, 900, 700, 100] 15 16L1=[]; L2=[]; L3=[]; L4=[] 17for x in range(0,14): 18 L1.append('Wf-%s' % x); L2.append('Wb-%s' % x); L3.append('Rf-%s' % x); L4.append('Rb-%s' % x) 19LB = L1 + L2 20RB = L3 + L4 21 22# 計算 23for i in range(0,5): 24 col1 = Ds1.columns.str.contains(r'(?=.*W)(?=.*%s)' % i) 25 col2 = Ds2.columns.str.contains(r'(?=.*W)(?=.*%s)' % i) 26 col3 = Ds1.columns.str.contains(r'(?=.*R)(?=.*%s)' % i) 27 col4 = Ds2.columns.str.contains(r'(?=.*R)(?=.*%s)' % i) 28 A1 = bL[i] + Ds2.loc[:, col2].values/2 * np.cos(Ds1.loc[:, col1].values*np.pi/180) 29 A2 = bL[i] - Ds2.loc[:, col2].values/2 * np.cos(Ds1.loc[:, col1].values*np.pi/180) 30 A3 = bL1[i] + Ds2.loc[:, col4].values/2 * np.cos(-Ds1.loc[:, col3].values*np.pi/180) 31 A4 = bL1[i] - Ds2.loc[:, col4].values/2 * np.cos(-Ds1.loc[:, col3].values*np.pi/180) 32 33 if i == 0: 34 B1 = A1.copy() 35 B2 = A2.copy() 36 B3 = A3.copy() 37 B4 = A4.copy() 38 else: 39 B1 = np.c_[B1, A1] # 列の結合 40 B2 = np.c_[B2, A2] 41 B3 = np.c_[B3, A3] 42 B4 = np.c_[B4, A4] 43 44for y in range(0,14): 45 A5 = hL[y] + Ds2.iloc[:, y].values/2 * np.sin(Ds1.iloc[:, y].values*np.pi/180) 46 A6 = hL[y] - Ds2.iloc[:, y].values/2 * np.sin(Ds1.iloc[:, y].values*np.pi/180) 47 A7 = hL[y] + Ds2.iloc[:, y+14].values/2 * np.sin(-Ds1.iloc[:, y+14].values*np.pi/180) 48 A8 = hL[y] - Ds2.iloc[:, y+14].values/2 * np.sin(-Ds1.iloc[:, y+14].values*np.pi/180) 49 50 if y == 0: 51 B5 = A5.copy() 52 B6 = A6.copy() 53 B7 = A7.copy() 54 B8 = A8.copy() 55 else: 56 B5 = np.c_[B5, A5] 57 B6 = np.c_[B6, A6] 58 B7 = np.c_[B7, A7] 59 B8 = np.c_[B8, A8] 60 61B_1 = np.c_[B1, B2] 62B_2 = np.c_[B5, B6] 63B_3 = np.c_[B3, B4] 64B_4 = np.c_[B7, B8] 65LcB = pd.DataFrame(B_1, columns=LB).fillna(0) 66LsB = pd.DataFrame(B_2, columns=LB).fillna(0) 67RcB = pd.DataFrame(B_3, columns=RB).fillna(0) 68RsB = pd.DataFrame(B_4, columns=RB).fillna(0) 69 70# LcBなどの配列のある行の抽出 71P = 5 72LBx = pd.DataFrame(LcB.T.loc[:, P].values.reshape((2,14))) 73LBy = pd.DataFrame(LsB.T.loc[:, P].values.reshape((2,14))) 74RBx = pd.DataFrame(RcB.T.loc[:, P].values.reshape((2,14))) 75RBy = pd.DataFrame(RsB.T.loc[:, P].values.reshape((2,14))) 76Bx = pd.concat((LBx, RBx), axis=1); Bx.columns=LB# x軸プロット用 77By = pd.concat((LBy, RBy), axis=1); By.columns=RB# y軸プロット用 78 79# グラフ 80%matplotlib notebook 81import matplotlib 82import matplotlib.pyplot as plt 83 84# グラフ描画 85fig = plt.figure() 86ax = fig.add_subplot(1, 1, 1) 87 88# プロット 89for i in range(0, 28): 90 ax.plot(Bx.loc[:,i], By.loc[:,i])

トレースバック

--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-4-3e697ed7e70a> in <module>() 88 # プロット 89 for i in range(0, 28): ---> 90 ax.plot(Bx.loc[:,i], By.loc[:,i]) C:lib\site-packages\pandas\core\indexing.py in __getitem__(self, key) 1308 1309 if type(key) is tuple: -> 1310 return self._getitem_tuple(key) 1311 else: 1312 return self._getitem_axis(key, axis=0) C:lib\site-packages\pandas\core\indexing.py in _getitem_tuple(self, tup) 794 def _getitem_tuple(self, tup): 795 try: --> 796 return self._getitem_lowerdim(tup) 797 except IndexingError: 798 pass C:lib\site-packages\pandas\core\indexing.py in _getitem_lowerdim(self, tup) 920 for i, key in enumerate(tup): 921 if is_label_like(key) or isinstance(key, tuple): --> 922 section = self._getitem_axis(key, axis=i) 923 924 # we have yielded a scalar ? C:lib\site-packages\pandas\core\indexing.py in _getitem_axis(self, key, axis) 1480 1481 # fall thru to straight lookup -> 1482 self._has_valid_type(key, axis) 1483 return self._get_label(key, axis=axis) 1484 C:lib\site-packages\pandas\core\indexing.py in _has_valid_type(self, key, axis) 1407 1408 try: -> 1409 key = self._convert_scalar_indexer(key, axis) 1410 if key not in ax: 1411 error() C:lib\site-packages\pandas\core\indexing.py in _convert_scalar_indexer(self, key, axis) 194 ax = self.obj._get_axis(min(axis, self.ndim - 1)) 195 # a scalar --> 196 return ax._convert_scalar_indexer(key, kind=self.name) 197 198 def _convert_slice_indexer(self, key, axis): C:lib\site-packages\pandas\indexes\base.py in _convert_scalar_indexer(self, key, kind) 1169 elif kind in ['loc'] and is_integer(key): 1170 if not self.holds_integer(): -> 1171 return self._invalid_indexer('label', key) 1172 1173 return key C:lib\site-packages\pandas\indexes\base.py in _invalid_indexer(self, form, key) 1282 "indexers [{key}] of {kind}".format( 1283 form=form, klass=type(self), key=key, -> 1284 kind=type(key))) 1285 1286 def get_duplicates(self): TypeError: cannot do label indexing on <class 'pandas.indexes.base.Index'> with these indexers [0] of <class 'int'>

以上,よろしくお願いいたします.

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

最後locとilocの入力をミスってました

投稿2018/11/22 03:47

shougi

総合スコア15

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問