Bar plot in inset plot (matplotlib) has problems when adding text as labels?

Question:

I need to plot a barplot in an inset axis from a subfigure. This is the code (a simplified version) to generate the subplot and the inset axis:

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])

It generates the following output:

enter image description here

But now, what I need is to insert the barpot inside the inset. To do that I add at the end ax_in.bar(labels,MAES). The output that I get is the same as before, and also the following error appears:

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']

Why the bar method works in an axis subplot and not in an inset axis? How can I solve this error?

Asked By: oscarcapote

||

Answers:

It seems like inset_axes does not play well with a list of strings for the x-axis values. To compensate for this, manually set the x-axis values to a list of int values with range(len(labels)) so that it works with inset_axes. Then, use the built in tick_label for plt.bar and set that equal to your actual x-axis values:

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)

# To make your x axis values essentially [0,1,2,3,4] (or however long your label list is)
x_value = range(len(labels))  

ax_in = axs[0].inset_axes([0.8, 0.1, 0.15, 0.15])
ax_in.bar(x_value,MAES, tick_label = labels)
plt.show()

Output:

enter image description here

Answered By: Michael S.
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.