简体   繁体   English

插入文本 plot(matplotlib)中的条形 plot 在将文本添加为标签时出现问题?

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

I need to plot a barplot in an inset axis from a subfigure.我需要 plot 从子图的插入轴中的条形图。 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:它生成以下 output:

在此处输入图像描述

But now, what I need is to insert the barpot inside the inset.但现在,我需要将 barpot 插入插图中。 To do that I add at the end ax_in.bar(labels,MAES) .为此,我在末尾添加ax_in.bar(labels,MAES) The output that I get is the same as before, and also the following error appears:我得到的output和之前的一样,还出现如下错误:

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?为什么 bar 方法适用于轴子图而不是插入轴? How can I solve this error?我该如何解决这个错误?

It seems like inset_axes does not play well with a list of strings for the x-axis values.似乎inset_axes不能很好地处理 x 轴值的strings列表。 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 .为了弥补这一点,手动将 x 轴值设置为具有range(len(labels))int值列表,以便它与inset_axes一起使用。 Then, use the built in tick_label for plt.bar and set that equal to your actual x-axis values:然后,使用plt.bar tick_label其设置为等于您的实际 x 轴值:

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: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM