使用seaborn在特定x轴点标记绘图

8zzbczxx  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(358)

我已经试着解决这个问题好几个小时了,我真的很沮丧。我只想在时间点2、6和10高亮显示我的绘图(如图所示)

我想创建多个绘图(我有不同的功能(y轴)和组(黑屏),我想要这些绘图)。
这是我的密码:

for elem in mylist:
    for title in myotherlist:
        to_mark = [False, True, False, False, False, True, False, False, False, True]
        to_mark2 = [1,5,9]

        sns.lineplot(data=data, x="time_point", y=elem, hue=title, err_style="bars", ci=95, markevery = [1,5,9], marker = '|', markersize=20, fillstyle='none', markeredgewidth=1.5, markeredgecolor='black')
        plt.savefig(file_path)
        plt.clf()
        plt.cla()
        plt.close()

我试过了

markevery = to_mark and
markevery = to_mark2

但似乎什么都不管用。我收到了错误信息

IndexError                                Traceback (most recent call last)
    ~\AppData\Roaming\Python\Python36\site-packages\matplotlib\lines.py in _mark_every_path(markevery,     tpath, affine, ax_transform)
    215         try:
--> 216             return Path(verts[markevery],
    217                     _slice_or_none(codes, markevery))

IndexError: index 5 is out of bounds for axis 0 with size 2

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-318-7c71faf85be6> in <module>()
     65 
     66         #plt.plot()
---> 67         plt.savefig(file_path)
     68         #with open(file_path, 'wb') as f:
     69             #plt.savefig(file_path)

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\pyplot.py in savefig(*args,**kwargs)
    708 def savefig(*args,**kwargs):
    709     fig = gcf()
--> 710     res = fig.savefig(*args,**kwargs)
    711     fig.canvas.draw_idle()   # need this if 'transparent=True' to reset colors
    712     return res

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\figure.py in savefig(self, fname,**kwargs)
   2033             self.set_frameon(frameon)
   2034 
-> 2035         self.canvas.print_figure(fname,**kwargs)
   2036 
   2037         if frameon:

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format,**kwargs)
   2261                 orientation=orientation,
   2262                 bbox_inches_restore=_bbox_inches_restore,
-> 2263               **kwargs)
   2264         finally:
   2265             if bbox_inches and restore_bbox:

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\backends\backend_pdf.py in print_pdf(self, filename,**kwargs)
   2584                 RendererPdf(file, image_dpi, height, width),
   2585                 bbox_inches_restore=_bbox_inches_restore)
-> 2586             self.figure.draw(renderer)
   2587             renderer.finalize()
   2588             if not isinstance(filename, PdfPages):

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args,**kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args,**kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\figure.py in draw(self, renderer)
   1473 
   1474             mimage._draw_list_compositing_images(
-> 1475                 renderer, self, artists, self.suppressComposite)
   1476 
   1477             renderer.close_group('figure')

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    139     if not_composite or not has_images:
    140         for a in artists:
--> 141             a.draw(renderer)
    142     else:
    143         # Composite any adjacent images together

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args,**kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args,**kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\axes\_base.py in draw(self, renderer, inframe)
   2605             renderer.stop_rasterizing()
   2606 
-> 2607         mimage._draw_list_compositing_images(renderer, self, artists)
   2608 
   2609         renderer.close_group('axes')

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    139     if not_composite or not has_images:
    140         for a in artists:
--> 141             a.draw(renderer)
    142     else:
    143         # Composite any adjacent images together

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args,**kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args,**kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\lines.py in draw(self, renderer)
    809                 if markevery is not None:
    810                     subsampled = _mark_every_path(markevery, tpath,
--> 811                                                   affine, self.axes.transAxes)
    812                 else:
    813                     subsampled = tpath

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\lines.py in _mark_every_path(markevery, tpath, affine, ax_transform)
    220             raise ValueError('`markevery` is iterable but '
    221                 'not a valid form of numpy fancy indexing; '
--> 222                 'markevery=%s' % (markevery,))
    223     else:
    224         raise ValueError('Value of `markevery` is not '

ValueError: `markevery` is iterable but not a valid form of numpy fancy indexing; markevery=[1, 5, 9]

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
~\AppData\Roaming\Python\Python36\site-packages\matplotlib\lines.py in _mark_every_path(markevery, tpath, affine, ax_transform)
    215         try:
--> 216             return Path(verts[markevery],
    217                     _slice_or_none(codes, markevery))

IndexError: index 5 is out of bounds for axis 0 with size 2

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
~\Anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    330                 pass
    331             else:
--> 332                 return printer(obj)
    333             # Finally look for special method names
    334             method = get_real_method(obj, self.print_method)

~\Anaconda3\lib\site-packages\IPython\core\pylabtools.py in <lambda>(fig)
    235 
    236     if 'png' in formats:
--> 237         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png',**kwargs))
    238     if 'retina' in formats or 'png2x' in formats:
    239         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)
    119 
    120     bytes_io = BytesIO()
--> 121     fig.canvas.print_figure(bytes_io,**kw)
    122     data = bytes_io.getvalue()
    123     if fmt == 'svg':

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format,**kwargs)
   2210                     orientation=orientation,
   2211                     dryrun=True,
-> 2212                   **kwargs)
   2213                 renderer = self.figure._cachedRenderer
   2214                 bbox_inches = self.figure.get_tightbbox(renderer)

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\backends\backend_agg.py in print_png(self, filename_or_obj, *args,**kwargs)
    511 
    512     def print_png(self, filename_or_obj, *args,**kwargs):
--> 513         FigureCanvasAgg.draw(self)
    514         renderer = self.get_renderer()
    515         original_dpi = renderer.dpi

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\backends\backend_agg.py in draw(self)
    431             # if toolbar:
    432             #     toolbar.set_cursor(cursors.WAIT)
--> 433             self.figure.draw(self.renderer)
    434             # A GUI class may be need to update a window using this draw, so
    435             # don't forget to call the superclass.

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args,**kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args,**kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\figure.py in draw(self, renderer)
   1473 
   1474             mimage._draw_list_compositing_images(
-> 1475                 renderer, self, artists, self.suppressComposite)
   1476 
   1477             renderer.close_group('figure')

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    139     if not_composite or not has_images:
    140         for a in artists:
--> 141             a.draw(renderer)
    142     else:
    143         # Composite any adjacent images together

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args,**kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args,**kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\axes\_base.py in draw(self, renderer, inframe)
   2605             renderer.stop_rasterizing()
   2606 
-> 2607         mimage._draw_list_compositing_images(renderer, self, artists)
   2608 
   2609         renderer.close_group('axes')

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    139     if not_composite or not has_images:
    140         for a in artists:
--> 141             a.draw(renderer)
    142     else:
    143         # Composite any adjacent images together

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args,**kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args,**kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\lines.py in draw(self, renderer)
    809                 if markevery is not None:
    810                     subsampled = _mark_every_path(markevery, tpath,
--> 811                                                   affine, self.axes.transAxes)
    812                 else:
    813                     subsampled = tpath

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\lines.py in _mark_every_path(markevery, tpath, affine, ax_transform)
    220             raise ValueError('`markevery` is iterable but '
    221                 'not a valid form of numpy fancy indexing; '
--> 222                 'markevery=%s' % (markevery,))
    223     else:
    224         raise ValueError('Value of `markevery` is not '

ValueError: `markevery` is iterable but not a valid form of numpy fancy indexing; markevery=[1, 5, 9]

如果我将markevery=设置为\u mark,我将得到

IndexError                                Traceback (most recent call last)
~\AppData\Roaming\Python\Python36\site-packages\matplotlib\lines.py in _mark_every_path(markevery, tpath, affine, ax_transform)
    215         try:
--> 216             return Path(verts[markevery],
    217                     _slice_or_none(codes, markevery))

IndexError: boolean index did not match indexed array along dimension 0; dimension is 2 but corresponding boolean dimension is 10

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-319-ce96ba06eb23> in <module>()
     65 
     66         #plt.plot()
---> 67         plt.savefig(file_path)
     68         #with open(file_path, 'wb') as f:
     69             #plt.savefig(file_path)

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\pyplot.py in savefig(*args,**kwargs)
    708 def savefig(*args,**kwargs):
    709     fig = gcf()
--> 710     res = fig.savefig(*args,**kwargs)
    711     fig.canvas.draw_idle()   # need this if 'transparent=True' to reset colors
    712     return res

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\figure.py in savefig(self, fname,**kwargs)
   2033             self.set_frameon(frameon)
   2034 
-> 2035         self.canvas.print_figure(fname,**kwargs)
   2036 
   2037         if frameon:

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format,**kwargs)
   2261                 orientation=orientation,
   2262                 bbox_inches_restore=_bbox_inches_restore,
-> 2263               **kwargs)
   2264         finally:
   2265             if bbox_inches and restore_bbox:

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\backends\backend_pdf.py in print_pdf(self, filename,**kwargs)
   2584                 RendererPdf(file, image_dpi, height, width),
   2585                 bbox_inches_restore=_bbox_inches_restore)
-> 2586             self.figure.draw(renderer)
   2587             renderer.finalize()
   2588             if not isinstance(filename, PdfPages):

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args,**kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args,**kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\figure.py in draw(self, renderer)
   1473 
   1474             mimage._draw_list_compositing_images(
-> 1475                 renderer, self, artists, self.suppressComposite)
   1476 
   1477             renderer.close_group('figure')

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    139     if not_composite or not has_images:
    140         for a in artists:
--> 141             a.draw(renderer)
    142     else:
    143         # Composite any adjacent images together

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args,**kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args,**kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\axes\_base.py in draw(self, renderer, inframe)
   2605             renderer.stop_rasterizing()
   2606 
-> 2607         mimage._draw_list_compositing_images(renderer, self, artists)
   2608 
   2609         renderer.close_group('axes')

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    139     if not_composite or not has_images:
    140         for a in artists:
--> 141             a.draw(renderer)
    142     else:
    143         # Composite any adjacent images together

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args,**kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args,**kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\lines.py in draw(self, renderer)
    809                 if markevery is not None:
    810                     subsampled = _mark_every_path(markevery, tpath,
--> 811                                                   affine, self.axes.transAxes)
    812                 else:
    813                     subsampled = tpath

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\lines.py in _mark_every_path(markevery, tpath, affine, ax_transform)
    220             raise ValueError('`markevery` is iterable but '
    221                 'not a valid form of numpy fancy indexing; '
--> 222                 'markevery=%s' % (markevery,))
    223     else:
    224         raise ValueError('Value of `markevery` is not '

ValueError: `markevery` is iterable but not a valid form of numpy fancy indexing; markevery=[False, True, False, False, False, True, False, False, False, True]

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
~\AppData\Roaming\Python\Python36\site-packages\matplotlib\lines.py in _mark_every_path(markevery, tpath, affine, ax_transform)
    215         try:
--> 216             return Path(verts[markevery],
    217                     _slice_or_none(codes, markevery))

IndexError: boolean index did not match indexed array along dimension 0; dimension is 2 but corresponding boolean dimension is 10

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
~\Anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    330                 pass
    331             else:
--> 332                 return printer(obj)
    333             # Finally look for special method names
    334             method = get_real_method(obj, self.print_method)

~\Anaconda3\lib\site-packages\IPython\core\pylabtools.py in <lambda>(fig)
    235 
    236     if 'png' in formats:
--> 237         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png',**kwargs))
    238     if 'retina' in formats or 'png2x' in formats:
    239         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)
    119 
    120     bytes_io = BytesIO()
--> 121     fig.canvas.print_figure(bytes_io,**kw)
    122     data = bytes_io.getvalue()
    123     if fmt == 'svg':

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format,**kwargs)
   2210                     orientation=orientation,
   2211                     dryrun=True,
-> 2212                   **kwargs)
   2213                 renderer = self.figure._cachedRenderer
   2214                 bbox_inches = self.figure.get_tightbbox(renderer)

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\backends\backend_agg.py in print_png(self, filename_or_obj, *args,**kwargs)
    511 
    512     def print_png(self, filename_or_obj, *args,**kwargs):
--> 513         FigureCanvasAgg.draw(self)
    514         renderer = self.get_renderer()
    515         original_dpi = renderer.dpi

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\backends\backend_agg.py in draw(self)
    431             # if toolbar:
    432             #     toolbar.set_cursor(cursors.WAIT)
--> 433             self.figure.draw(self.renderer)
    434             # A GUI class may be need to update a window using this draw, so
    435             # don't forget to call the superclass.

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args,**kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args,**kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\figure.py in draw(self, renderer)
   1473 
   1474             mimage._draw_list_compositing_images(
-> 1475                 renderer, self, artists, self.suppressComposite)
   1476 
   1477             renderer.close_group('figure')

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    139     if not_composite or not has_images:
    140         for a in artists:
--> 141             a.draw(renderer)
    142     else:
    143         # Composite any adjacent images together

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args,**kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args,**kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\axes\_base.py in draw(self, renderer, inframe)
   2605             renderer.stop_rasterizing()
   2606 
-> 2607         mimage._draw_list_compositing_images(renderer, self, artists)
   2608 
   2609         renderer.close_group('axes')

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    139     if not_composite or not has_images:
    140         for a in artists:
--> 141             a.draw(renderer)
    142     else:
    143         # Composite any adjacent images together

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args,**kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args,**kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\lines.py in draw(self, renderer)
    809                 if markevery is not None:
    810                     subsampled = _mark_every_path(markevery, tpath,
--> 811                                                   affine, self.axes.transAxes)
    812                 else:
    813                     subsampled = tpath

~\AppData\Roaming\Python\Python36\site-packages\matplotlib\lines.py in _mark_every_path(markevery, tpath, affine, ax_transform)
    220             raise ValueError('`markevery` is iterable but '
    221                 'not a valid form of numpy fancy indexing; '
--> 222                 'markevery=%s' % (markevery,))
    223     else:
    224         raise ValueError('Value of `markevery` is not '

ValueError: `markevery` is iterable but not a valid form of numpy fancy indexing; markevery=[False, True, False, False, False, True, False, False, False, True]

我将非常感谢您的任何意见!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题