fig, axs = plt.subplots(2,2, figsize=(10,6))
fig.subplots_adjust(hspace=0.5, wspace=0.3)
axs[0,0].plot(df.loc['Brasil', anos])
axs[0,0].set_title('Brasil')
axs[0,1].plot(df.loc['Colômbia', anos])
axs[0,1].set_title('Colômbia')
axs[1,0].plot(df.loc['Argentina', anos])
axs[1,0].set_title('Argentina')
axs[1,1].plot(df.loc['Peru', anos])
axs[1,1].set_title('Peru')
for ax in axs.flat:
ax.xaxis.set_major_locator(plt.MultipleLocator(5))
for ax in axs.flat:
ax.set_xlabel('Ano')
ax.set_ylabel('Número de imigrantes')
plt.show()
O erro que aparece...
KeyError Traceback (most recent call last)
Cell In[76], line 4
1 fig, axs = plt.subplots(2,2, figsize=(10,6))
2 fig.subplots_adjust(hspace=0.5, wspace=0.3)
----> 4 axs[0,0].plot(df.loc['Brasil', anos])
5 axs[0,0].set_title('Brasil')
7 axs[0,1].plot(df.loc['Colômbia', anos])
File ~\anaconda3\lib\site-packages\pandas\core\indexing.py:1067, in _LocationIndexer.__getitem__(self, key)
1065 if self._is_scalar_access(key):
1066 return self.obj._get_value(*key, takeable=self._takeable)
-> 1067 return self._getitem_tuple(key)
1068 else:
1069 # we by definition only have the 0th axis
1070 axis = self.axis or 0
File ~\anaconda3\lib\site-packages\pandas\core\indexing.py:1247, in _LocIndexer._getitem_tuple(self, tup)
1245 with suppress(IndexingError):
1246 tup = self._expand_ellipsis(tup)
-> 1247 return self._getitem_lowerdim(tup)
1249 # no multi-index, so validate all of the indexers
1250 tup = self._validate_tuple_indexer(tup)
File ~\anaconda3\lib\site-packages\pandas\core\indexing.py:967, in _LocationIndexer._getitem_lowerdim(self, tup)
963 for i, key in enumerate(tup):
964 if is_label_like(key):
965 # We don't need to check for tuples here because those are
966 # caught by the _is_nested_tuple_indexer check above.
--> 967 section = self._getitem_axis(key, axis=i)
969 # We should never have a scalar section here, because
970 # _getitem_lowerdim is only called after a check for
971 # is_scalar_access, which that would be.
972 if section.ndim == self.ndim:
973 # we're in the middle of slicing through a MultiIndex
974 # revise the key wrt to `section` by inserting an _NS
File ~\anaconda3\lib\site-packages\pandas\core\indexing.py:1312, in _LocIndexer._getitem_axis(self, key, axis)
1310 # fall thru to straight lookup
1311 self._validate_key(key, axis)
-> 1312 return self._get_label(key, axis=axis)
File ~\anaconda3\lib\site-packages\pandas\core\indexing.py:1260, in _LocIndexer._get_label(self, label, axis)
1258 def _get_label(self, label, axis: int):
1259 # GH#5567 this will fail if the label is not present in the axis.
-> 1260 return self.obj.xs(label, axis=axis)
File ~\anaconda3\lib\site-packages\pandas\core\generic.py:4056, in NDFrame.xs(self, key, axis, level, drop_level)
4054 new_index = index[loc]
4055 else:
-> 4056 loc = index.get_loc(key)
4058 if isinstance(loc, np.ndarray):
4059 if loc.dtype == np.bool_:
File ~\anaconda3\lib\site-packages\pandas\core\indexes\range.py:395, in RangeIndex.get_loc(self, key, method, tolerance)
393 raise KeyError(key) from err
394 self._check_indexing_error(key)
--> 395 raise KeyError(key)
396 return super().get_loc(key, method=method, tolerance=tolerance)
KeyError: 'Brasil'