Python TypeError:float()参数必须是字符串或数字,而不是带有注释和ItError的“tuple”
我想根据月/年绘制平均值Python TypeError:float()参数必须是字符串或数字,而不是带有注释和ItError的“tuple”,python,pandas,matplotlib,Python,Pandas,Matplotlib,我想根据月/年绘制平均值 data=df.groupby([df.index.year.rename('year'),df.index.month.rename('month')]).mean() 这是我的数据样本和日期索引 mean count date 1901-01-31 0.608475 3 1901-02-28 0.751826 6 1901-03-31 0.050980 6 1901-04-30 0.229
data=df.groupby([df.index.year.rename('year'),df.index.month.rename('month')]).mean()
这是我的数据样本和日期索引
mean count
date
1901-01-31 0.608475 3
1901-02-28 0.751826 6
1901-03-31 0.050980 6
1901-04-30 0.229060 2
1901-05-31 0.246976 4
1901-11-30 0.936585 8
1901-12-31 0.085918 1
1902-07-31 0.786897 0
1902-08-31 0.774024 9
1902-12-31 0.602361 6
1903-01-31 0.805497 4
1903-02-28 0.604498 7
1903-03-31 0.991389 2
1903-04-30 0.774783 1
1903-05-31 0.880222 9
1903-06-30 0.735544 1
因此,我按月/按年向groupby编写了这段代码
data=df.groupby([df.index.year.rename('year'),df.index.month.rename('month')]).mean()
下面是分组后的数据示例
mean count
date date
1901 1 0.468082 7
2 0.378744 8
3 0.668791 3
4 0.563842 7
5 0.764414 2
6 0.530849 3
7 0.703855 7
8 0.604860 9
9 0.766420 0
10 0.752520 8
11 0.361177 3
12 0.502622 1
1902 1 0.694125 0
2 0.661784 0
3 0.491230 2
4 0.074626 7
5 0.291552 4
6 0.376639 6
7 0.681985 8
8 0.381116 6
9 0.895843 3
10 0.378515 8
11 0.803789 8
12 0.616002 7
1903 1 0.372718 4
2 0.992567 9
3 0.746151 9
4 0.970088 7
5 0.053416 1
6 0.959746 4
7 0.604006 0
8 0.894918 0
9 0.716278 3
10 0.420532 6
11 0.161469 7
那我就有阴谋了
import matplotlib.pyplot as plt
plt.figure()
ax = data['mean'].plot(figsize=(20,10),linewidth=0.8)
for d,row in data.iterrows():
ax.annotate('{:.0f}'.format(row['count']), xy=(d,row['mean']), ha='center',clip_on=True)
但我犯了这个错误
Error in callback <function install_repl_displayhook.<locals>.post_execute at 0x00000250AD5FDD08> (for post_execute):
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\matplotlib\pyplot.py in post_execute()
107 def post_execute():
108 if matplotlib.is_interactive():
--> 109 draw_all()
110
111 # IPython >= 2
~\Anaconda3\lib\site-packages\matplotlib\_pylab_helpers.py in draw_all(cls, force)
126 for f_mgr in cls.get_all_fig_managers():
127 if force or f_mgr.canvas.figure.stale:
--> 128 f_mgr.canvas.draw_idle()
129
130 atexit.register(Gcf.destroy_all)
~\Anaconda3\lib\site-packages\matplotlib\backend_bases.py in draw_idle(self, *args, **kwargs)
1905 if not self._is_idle_drawing:
1906 with self._idle_draw_cntx():
-> 1907 self.draw(*args, **kwargs)
1908
1909 def draw_cursor(self, event):
~\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in draw(self)
386 self.renderer = self.get_renderer(cleared=True)
387 with RendererAgg.lock:
--> 388 self.figure.draw(self.renderer)
389 # A GUI class may be need to update a window using this draw, so
390 # don't forget to call the superclass.
~\Anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
36 renderer.start_filter()
37
---> 38 return draw(artist, renderer, *args, **kwargs)
39 finally:
40 if artist.get_agg_filter() is not None:
~\Anaconda3\lib\site-packages\matplotlib\figure.py in draw(self, renderer)
1707 self.patch.draw(renderer)
1708 mimage._draw_list_compositing_images(
-> 1709 renderer, self, artists, self.suppressComposite)
1710
1711 renderer.close_group('figure')
~\Anaconda3\lib\site-packages\matplotlib\image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
133 if not_composite or not has_images:
134 for a in artists:
--> 135 a.draw(renderer)
136 else:
137 # Composite any adjacent images together
~\Anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
36 renderer.start_filter()
37
---> 38 return draw(artist, renderer, *args, **kwargs)
39 finally:
40 if artist.get_agg_filter() is not None:
~\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in draw(self, renderer, inframe)
2643 renderer.stop_rasterizing()
2644
-> 2645 mimage._draw_list_compositing_images(renderer, self, artists)
2646
2647 renderer.close_group('axes')
~\Anaconda3\lib\site-packages\matplotlib\image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
133 if not_composite or not has_images:
134 for a in artists:
--> 135 a.draw(renderer)
136 else:
137 # Composite any adjacent images together
~\Anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
36 renderer.start_filter()
37
---> 38 return draw(artist, renderer, *args, **kwargs)
39 finally:
40 if artist.get_agg_filter() is not None:
~\Anaconda3\lib\site-packages\matplotlib\text.py in draw(self, renderer)
2353 return
2354
-> 2355 xy_pixel = self._get_position_xy(renderer)
2356 if not self._check_xy(renderer, xy_pixel):
2357 return
~\Anaconda3\lib\site-packages\matplotlib\text.py in _get_position_xy(self, renderer)
1903 "Return the pixel position of the annotated point."
1904 x, y = self.xy
-> 1905 return self._get_xy(renderer, x, y, self.xycoords)
1906
1907 def _check_xy(self, renderer, xy_pixel):
~\Anaconda3\lib\site-packages\matplotlib\text.py in _get_xy(self, renderer, x, y, s)
1757
1758 if s1 == 'data':
-> 1759 x = float(self.convert_xunits(x))
1760 if s2 == 'data':
1761 y = float(self.convert_yunits(y))
TypeError: float() argument must be a string or a number, not 'tuple'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
339 pass
340 else:
--> 341 return printer(obj)
342 # Finally look for special method names
343 method = get_real_method(obj, self.print_method)
~\Anaconda3\lib\site-packages\IPython\core\pylabtools.py in <lambda>(fig)
242
243 if 'png' in formats:
--> 244 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
245 if 'retina' in formats or 'png2x' in formats:
246 png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))
~\Anaconda3\lib\site-packages\IPython\core\pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
126
127 bytes_io = BytesIO()
--> 128 fig.canvas.print_figure(bytes_io, **kw)
129 data = bytes_io.getvalue()
130 if fmt == 'svg':
~\Anaconda3\lib\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, **kwargs)
2054 orientation=orientation,
2055 dryrun=True,
-> 2056 **kwargs)
2057 renderer = self.figure._cachedRenderer
2058 bbox_artists = kwargs.pop("bbox_extra_artists", None)
~\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in print_png(self, filename_or_obj, metadata, pil_kwargs, *args, **kwargs)
525
526 else:
--> 527 FigureCanvasAgg.draw(self)
528 renderer = self.get_renderer()
529 with cbook._setattr_cm(renderer, dpi=self.figure.dpi), \
~\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in draw(self)
386 self.renderer = self.get_renderer(cleared=True)
387 with RendererAgg.lock:
--> 388 self.figure.draw(self.renderer)
389 # A GUI class may be need to update a window using this draw, so
390 # don't forget to call the superclass.
~\Anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
36 renderer.start_filter()
37
---> 38 return draw(artist, renderer, *args, **kwargs)
39 finally:
40 if artist.get_agg_filter() is not None:
~\Anaconda3\lib\site-packages\matplotlib\figure.py in draw(self, renderer)
1707 self.patch.draw(renderer)
1708 mimage._draw_list_compositing_images(
-> 1709 renderer, self, artists, self.suppressComposite)
1710
1711 renderer.close_group('figure')
~\Anaconda3\lib\site-packages\matplotlib\image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
133 if not_composite or not has_images:
134 for a in artists:
--> 135 a.draw(renderer)
136 else:
137 # Composite any adjacent images together
~\Anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
36 renderer.start_filter()
37
---> 38 return draw(artist, renderer, *args, **kwargs)
39 finally:
40 if artist.get_agg_filter() is not None:
~\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in draw(self, renderer, inframe)
2643 renderer.stop_rasterizing()
2644
-> 2645 mimage._draw_list_compositing_images(renderer, self, artists)
2646
2647 renderer.close_group('axes')
~\Anaconda3\lib\site-packages\matplotlib\image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
133 if not_composite or not has_images:
134 for a in artists:
--> 135 a.draw(renderer)
136 else:
137 # Composite any adjacent images together
~\Anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
36 renderer.start_filter()
37
---> 38 return draw(artist, renderer, *args, **kwargs)
39 finally:
40 if artist.get_agg_filter() is not None:
~\Anaconda3\lib\site-packages\matplotlib\text.py in draw(self, renderer)
2353 return
2354
-> 2355 xy_pixel = self._get_position_xy(renderer)
2356 if not self._check_xy(renderer, xy_pixel):
2357 return
~\Anaconda3\lib\site-packages\matplotlib\text.py in _get_position_xy(self, renderer)
1903 "Return the pixel position of the annotated point."
1904 x, y = self.xy
-> 1905 return self._get_xy(renderer, x, y, self.xycoords)
1906
1907 def _check_xy(self, renderer, xy_pixel):
~\Anaconda3\lib\site-packages\matplotlib\text.py in _get_xy(self, renderer, x, y, s)
1757
1758 if s1 == 'data':
-> 1759 x = float(self.convert_xunits(x))
1760 if s2 == 'data':
1761 y = float(self.convert_yunits(y))
TypeError: float() argument must be a string or a number, not 'tuple'
<Figure size 1440x720 with 1 Axes>
想一想可能是因为分组方式的错误,如果我不按月份和年份分组数据,那么将其绘制为日期,如1901-01-31格式,它起作用了
所以我试着用use.reset_索引来解组,但是X标签是数据的索引,我想要的是月/年
此外,我还尝试通过使用将多索引合并为一个索引
data.index=['{}-{}'。formatii,j代表i,j在data.index中]
但我得到了这个
ConversionError:无法将值转换为轴单位:“1901-1”
预期结果与此类似,但以月/年为单位,而不是以天为单位
有什么帮助吗?您的示例数据帧,确保索引是日期时间数据类型
#imports excluded
d = {'mean':{'1901-01-31': 0.60847, '1901-02-28': 0.75183, '1901-03-31': 0.05098,
'1901-04-30': 0.22906, '1901-05-31': 0.24698, '1901-11-30': 0.93659,
'1901-12-31': 0.08592, '1902-07-31': 0.7869, '1902-08-31': 0.77402,
'1902-12-31': 0.60236, '1903-01-31': 0.8055, '1903-02-28': 0.6045,
'1903-03-31': 0.99139, '1903-04-30': 0.77478, '1903-05-31': 0.88022,
'1903-06-30': 0.73554},
'count': {'1901-01-31': 3, '1901-02-28': 6, '1901-03-31': 6, '1901-04-30': 2,
'1901-05-31': 4, '1901-11-30': 8, '1901-12-31': 1, '1902-07-31': 0,
'1902-08-31': 9, '1902-12-31': 6, '1903-01-31': 4, '1903-02-28': 7,
'1903-03-31': 2, '1903-04-30': 1, '1903-05-31': 9, '1903-06-30': 1}}
df = pd.DataFrame(d)
df.index = pd.to_datetime(df.index)
#df = df.set_index(pd.to_datetime(df.index))
xy参数需要两个浮点元组。按年度和月份分组后;对行进行迭代会生成一个年、月、浮点元组,因为多索引注释不喜欢这样
我还没有弄明白如何让groupby解决方案发挥作用——我认为这将涉及将年、月元组转换为绘图/轴理解的坐标,或者指定可以使用该元组的x坐标系
按月绘制和注释作品
data = df.resample('M').mean()
# or maybe it should be
#data = df.resample('M').agg({'mean':'mean','count':'sum'})
ax = data['mean'].plot()
for x,y,count in zip(data.index,data['mean'],data['count']):
ax.annotate(f'{count:.0f}',xy=(x,y), ha='center',clip_on=True)
# or
for (x,(y,count)) in data.iterrows():
ax.annotate(f'{count:.0f}',xy=(x,y), ha='center',clip_on=True)
plt.show()
plt.close()
示例数据框中缺少月份,因此此图具有不连续性。在发布有关产生异常的代码的问题时,始终包含完整的回溯-复制并粘贴它,然后将其格式化为代码选择它并键入ctrl键,在分组之前可能包含原始数据帧的最小示例。我向问题添加了一个问题:您希望每年单独绘制或子绘图,还是希望每年都有一条x轴为月的线?一条线x轴为月/年的绘图和线条谢谢,但在我的代码中,这部分plt.figure ax=data['mean'].plotfigsize=20,10,linewidth=0.8是有效的,即使我按问题分组,这部分是d,数据中的行。ItError:ax.注释“{.0f}”。formatrow['count'],xy=d,row['mean'],ha='center',clip_on=True