我有一个2D数据,并将其绘制在热图中,如(example heatmap)。 x轴目前是 matplotlib.ticker.FixedFormatter ,我想将其更改为 matplotlib.dates.DateFormatter 并仅显示年份。
from matplotlib import dates as mdates
ax = sns.heatmap(df[::4][['Total','Professionals']].transpose())
# ax.xaxis.set_major_formatter(mdates.DateFormatter('%y'))
plt.show()
然而,当我取消注释第三行时,我收到以下错误。
ValueError Traceback (most recent call last)
C:\Users\Chatdanai Lumdee\Anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
305 pass
306 else:
--> 307 return printer(obj)
308 # Finally look for special method names
309 method = get_real_method(obj, self.print_method)
C:\Users\Chatdanai Lumdee\Anaconda3\lib\site-packages\IPython\core\pylabtools.py in <lambda>(fig)
225
226 if 'png' in formats:
--> 227 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
228 if 'retina' in formats or 'png2x' in formats:
229 png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))
C:\Users\Chatdanai Lumdee\Anaconda3\lib\site-packages\IPython\core\pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
117
118 bytes_io = BytesIO()
--> 119 fig.canvas.print_figure(bytes_io, **kw)
120 data = bytes_io.getvalue()
121 if fmt == 'svg':
C:\Users\Chatdanai Lumdee\Anaconda3\lib\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
2178 orientation=orientation,
2179 dryrun=True,
-> 2180 **kwargs)
2181 renderer = self.figure._cachedRenderer
2182 bbox_inches = self.figure.get_tightbbox(renderer)
C:\Users\Chatdanai Lumdee\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in print_png(self, filename_or_obj, *args, **kwargs)
525
526 def print_png(self, filename_or_obj, *args, **kwargs):
--> 527 FigureCanvasAgg.draw(self)
528 renderer = self.get_renderer()
529 original_dpi = renderer.dpi
C:\Users\Chatdanai Lumdee\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in draw(self)
472
473 try:
--> 474 self.figure.draw(self.renderer)
475 finally:
476 RendererAgg.lock.release()
C:\Users\Chatdanai Lumdee\Anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
60 def draw_wrapper(artist, renderer, *args, **kwargs):
61 before(artist, renderer)
---> 62 draw(artist, renderer, *args, **kwargs)
63 after(artist, renderer)
64
C:\Users\Chatdanai Lumdee\Anaconda3\lib\site-packages\matplotlib\figure.py in draw(self, renderer)
1157 dsu.sort(key=itemgetter(0))
1158 for zorder, a, func, args in dsu:
-> 1159 func(*args)
1160
1161 renderer.close_group('figure')
C:\Users\Chatdanai Lumdee\Anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
60 def draw_wrapper(artist, renderer, *args, **kwargs):
61 before(artist, renderer)
---> 62 draw(artist, renderer, *args, **kwargs)
63 after(artist, renderer)
64
C:\Users\Chatdanai Lumdee\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in draw(self, renderer, inframe)
2317
2318 for zorder, a in dsu:
-> 2319 a.draw(renderer)
2320
2321 renderer.close_group('axes')
C:\Users\Chatdanai Lumdee\Anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
60 def draw_wrapper(artist, renderer, *args, **kwargs):
61 before(artist, renderer)
---> 62 draw(artist, renderer, *args, **kwargs)
63 after(artist, renderer)
64
C:\Users\Chatdanai Lumdee\Anaconda3\lib\site-packages\matplotlib\axis.py in draw(self, renderer, *args, **kwargs)
1106 renderer.open_group(__name__)
1107
-> 1108 ticks_to_draw = self._update_ticks(renderer)
1109 ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,
1110 renderer)
C:\Users\Chatdanai Lumdee\Anaconda3\lib\site-packages\matplotlib\axis.py in _update_ticks(self, renderer)
949
950 interval = self.get_view_interval()
--> 951 tick_tups = [t for t in self.iter_ticks()]
952 if self._smart_bounds:
953 # handle inverted limits
C:\Users\Chatdanai Lumdee\Anaconda3\lib\site-packages\matplotlib\axis.py in <listcomp>(.0)
949
950 interval = self.get_view_interval()
--> 951 tick_tups = [t for t in self.iter_ticks()]
952 if self._smart_bounds:
953 # handle inverted limits
C:\Users\Chatdanai Lumdee\Anaconda3\lib\site-packages\matplotlib\axis.py in iter_ticks(self)
896 self.major.formatter.set_locs(majorLocs)
897 majorLabels = [self.major.formatter(val, i)
--> 898 for i, val in enumerate(majorLocs)]
899
900 minorLocs = self.minor.locator()
C:\Users\Chatdanai Lumdee\Anaconda3\lib\site-packages\matplotlib\axis.py in <listcomp>(.0)
896 self.major.formatter.set_locs(majorLocs)
897 majorLabels = [self.major.formatter(val, i)
--> 898 for i, val in enumerate(majorLocs)]
899
900 minorLocs = self.minor.locator()
C:\Users\Chatdanai Lumdee\Anaconda3\lib\site-packages\matplotlib\dates.py in __call__(self, x, pos)
482 'you have not informed the axis that it is '
483 'plotting dates, e.g., with ax.xaxis_date()')
--> 484 dt = num2date(x, self.tz)
485 return self.strftime(dt, self.fmt)
486
C:\Users\Chatdanai Lumdee\Anaconda3\lib\site-packages\matplotlib\dates.py in num2date(x, tz)
417 tz = _get_rc_timezone()
418 if not cbook.iterable(x):
--> 419 return _from_ordinalf(x, tz)
420 else:
421 x = np.asarray(x)
C:\Users\Chatdanai Lumdee\Anaconda3\lib\site-packages\matplotlib\dates.py in _from_ordinalf(x, tz)
269
270 ix = int(x)
--> 271 dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC)
272
273 remainder = float(x) - ix
ValueError: ordinal must be >= 1
有谁知道我该怎么做才能绕过这个?我尝试使用例如重新格式化x轴数据。 date2num首先在绘图之前,但在尝试重新格式化轴时仍会出现相同的错误。