python seaborn boxplot_python - 如何将文本框添加到Seaborn Boxplot? - 堆栈内存溢出

我试图在框图中插入一个文本框,但是出了点问题。 让我们先绘制箱线图:

import pandas as pd

import matplotlib.pyplot as plt

import seaborn as sb

df_data = pd.read_hdf('data.h5', key=f'df_data')

##print(df_data)

fig, (ax1, ax2) = plt.subplots(1,

2,

gridspec_kw={'width_ratios': [3, 1]})

my_pal = {'PtP':'dodgerblue', 'T1':'salmon', 'T2':'salmon', 'E':'limegreen'}

sb.boxplot(data=df_data[['PtP', 'T1', 'T2']], ax=ax1, palette=my_pal)

sb.boxplot(data=df_data[['E']], ax=ax2, palette=my_pal)

posx = 1500

posy = 'T1'

##ax1.text(posx,

## posy,

## f'Average Impulse Interval: Over9000',

## bbox=dict(facecolor='wheat'))

ax1.set_ylim(0)

ax1.set_ylabel('Voltage / mV, Time / ns')

ax2.set_ylim(0)

ax2.set_ylabel('Energy / fJ')

fig.suptitle('Distribution')

plt.grid(axis='y')

plt.show()

现在,当我尝试取消注释时:

ax1.text(posx,

posy,

f'Average Impulse Interval: Over9000',

bbox=dict(facecolor='wheat'))

Matplotlib引发错误:

Exception in Tkinter callback

Traceback (most recent call last):

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axis.py", line 1550, in convert_units

ret = self.converter.convert(x, self.units, self)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\category.py", line 52, in convert

'Missing category information for StrCategoryConverter; '

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:

Traceback (most recent call last):

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__

return self.func(*args)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\backends\_backend_tk.py", line 259, in resize

self.draw()

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 9, in draw

super(FigureCanvasTkAgg, self).draw()

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\backends\backend_agg.py", line 388, in draw

self.figure.draw(self.renderer)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper

return draw(artist, renderer, *args, **kwargs)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\figure.py", line 1709, in draw

renderer, self, artists, self.suppressComposite)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\image.py", line 135, in _draw_list_compositing_images

a.draw(renderer)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper

return draw(artist, renderer, *args, **kwargs)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axes\_base.py", line 2647, in draw

mimage._draw_list_compositing_images(renderer, self, artists)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\image.py", line 135, in _draw_list_compositing_images

a.draw(renderer)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper

return draw(artist, renderer, *args, **kwargs)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\text.py", line 670, in draw

bbox, info, descent = textobj._get_layout(renderer)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\text.py", line 276, in _get_layout

key = self.get_prop_tup(renderer=renderer)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\text.py", line 831, in get_prop_tup

x, y = self.get_unitless_position()

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\text.py", line 814, in get_unitless_position

y = float(self.convert_yunits(self._y))

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\artist.py", line 192, in convert_yunits

return ax.yaxis.convert_units(y)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axis.py", line 1553, in convert_units

f'units: {x!r}') from e

matplotlib.units.ConversionError: Failed to convert value(s) to axis units: 'T1'

Exception in Tkinter callback

Traceback (most recent call last):

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axis.py", line 1550, in convert_units

ret = self.converter.convert(x, self.units, self)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\category.py", line 52, in convert

'Missing category information for StrCategoryConverter; '

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:

Traceback (most recent call last):

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__

return self.func(*args)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 749, in callit

func(*args)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\backends\_backend_tk.py", line 338, in idle_draw

self.draw()

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 9, in draw

super(FigureCanvasTkAgg, self).draw()

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\backends\backend_agg.py", line 388, in draw

self.figure.draw(self.renderer)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper

return draw(artist, renderer, *args, **kwargs)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\figure.py", line 1709, in draw

renderer, self, artists, self.suppressComposite)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\image.py", line 135, in _draw_list_compositing_images

a.draw(renderer)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper

return draw(artist, renderer, *args, **kwargs)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axes\_base.py", line 2647, in draw

mimage._draw_list_compositing_images(renderer, self, artists)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\image.py", line 135, in _draw_list_compositing_images

a.draw(renderer)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper

return draw(artist, renderer, *args, **kwargs)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\text.py", line 670, in draw

bbox, info, descent = textobj._get_layout(renderer)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\text.py", line 276, in _get_layout

key = self.get_prop_tup(renderer=renderer)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\text.py", line 831, in get_prop_tup

x, y = self.get_unitless_position()

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\text.py", line 814, in get_unitless_position

y = float(self.convert_yunits(self._y))

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\artist.py", line 192, in convert_yunits

return ax.yaxis.convert_units(y)

File "C:\Users\Artur\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axis.py", line 1553, in convert_units

f'units: {x!r}') from e

matplotlib.units.ConversionError: Failed to convert value(s) to axis units: 'T1'

我尝试用整数或浮点数替换'T1' ,但这无济于事。

不相关似乎我需要在此帖子中添加更多文本。 不要因为我在这里输入的废话而分心。

你可能感兴趣的:(python,seaborn,boxplot)