[英]Bar plot in inset plot (matplotlib) has problems when adding text as labels?
我需要 plot 從子圖的插入軸中的條形圖。 這是生成子圖和插入軸的代碼(簡化版本):
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])
它生成以下 output:
但現在,我需要將 barpot 插入插圖中。 為此,我在末尾添加ax_in.bar(labels,MAES)
。 我得到的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']
為什么 bar 方法適用於軸子圖而不是插入軸? 我該如何解決這個錯誤?
似乎inset_axes
不能很好地處理 x 軸值的strings
列表。 為了彌補這一點,手動將 x 軸值設置為具有range(len(labels))
的int
值列表,以便它與inset_axes
一起使用。 然后,使用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:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.