我需要从子图形在插入轴上绘制条形图.以下是生成子图和插入轴的代码(简化版本):
labels = ['A', 'B', 'C', 'D', 'E']
MAES = [np.random.rand() for i in range(len(labels))]
X = np.arange(0,4,.01)
Y = np.sin(X)
fig, axs = plt.subplots(2)
fig.suptitle('Vertically stacked subplots')
axs[0].plot(X,Y)
axs[1].bar(labels,MAES)
ax_in = axs[0].inset_axes([0.8, 0.1, 0.15, 0.15])
它会生成以下输出:
但现在,我需要做的是将罐子插入插图中.要做到这一点,我在最后ax_in.bar(labels,MAES)
处加了一句.我得到的输出与之前相同,并且还出现以下错误:
ValueError Traceback (most recent call last)
File /usr/local/lib/python3.8/dist-packages/matplotlib/axis.py:1506, in Axis.convert_units(self, x)
1505 try:
-> 1506 ret = self.converter.convert(x, self.units, self)
1507 except Exception as e:
File /usr/local/lib/python3.8/dist-packages/matplotlib/category.py:49, in StrCategoryConverter.convert(value, unit, axis)
48 if unit is None:
---> 49 raise ValueError(
50 'Missing category information for StrCategoryConverter; '
51 'this might be caused by unintendedly mixing categorical and '
52 'numeric data')
53 StrCategoryConverter._validate_unit(unit)
ValueError: Missing category information for StrCategoryConverter; this might be caused by unintendedly mixing categorical and numeric data
The above exception was the direct cause of the following exception:
ConversionError Traceback (most recent call last)
Input In [120], in <cell line: 13>()
10 axs[1].bar(labels,MAES)
12 ax_in = axs[0].inset_axes([0.8, 0.1, 0.15, 0.15])
---> 13 ax_in.bar(labels,MAES)
File /usr/local/lib/python3.8/dist-packages/matplotlib/__init__.py:1412, in inner(ax, data, *args, **kwargs)
1404 assert {*arg_names}.issuperset(replace_names or []) or varkwargs_name, (
1405 "Matplotlib internal error: invalid replace_names ({!r}) for {!r}"
1406 .format(replace_names, func.__name__))
1407 assert label_namer is None or label_namer in arg_names, (
1408 "Matplotlib internal error: invalid label_namer ({!r}) for {!r}"
1409 .format(label_namer, func.__name__))
1411 @functools.wraps(func)
-> 1412 def inner(ax, *args, data=None, **kwargs):
1413 if data is None:
1414 return func(ax, *map(sanitize_sequence, args), **kwargs)
File /usr/local/lib/python3.8/dist-packages/matplotlib/axes/_axes.py:2331, in bar(self, x, height, width, bottom, align, **kwargs)
File /usr/local/lib/python3.8/dist-packages/matplotlib/artist.py:252, in convert_xunits(self, x)
250 ax = getattr(self, 'axes', None)
251 if ax is None or ax.xaxis is None:
--> 252 return x
253 return ax.xaxis.convert_units(x)
File /usr/local/lib/python3.8/dist-packages/matplotlib/axis.py:1508, in Axis.convert_units(self, x)
1506 ret = self.converter.convert(x, self.units, self)
1507 except Exception as e:
-> 1508 raise munits.ConversionError('Failed to convert value(s) to axis '
1509 f'units: {x!r}') from e
1510 return ret
ConversionError: Failed to convert value(s) to axis units: ['A', 'B', 'C', 'D', 'E']
为什么条形图方法在轴子图中工作,而不是在插入轴中工作?我如何才能解决这个错误?