如果我有一个基于日期的x轴,如何使用ticker API在所需位置添加刻度线?使用FixedLocator
无效并导致异常:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker
dates = pd.date_range('20130101',periods=6)
series = pd.Series([1,3,5,8,6,8], index=dates)
p = plt.figure()
series.plot(ax=p.gca())
p.gca().xaxis.set_major_locator(matplotlib.ticker.FixedLocator(dates))
结果:
Traceback (most recent call last):
File "/media/local_data/languitar/miniconda2/envs/monitoring/lib/python2.7/site-packages/matplotlib/backends/backend
_qt5agg.py", line 176, in __draw_idle_agg
FigureCanvasAgg.draw(self)
File "/media/local_data/languitar/miniconda2/envs/monitoring/lib/python2.7/site-packages/matplotlib/backends/backend
_agg.py", line 474, in draw
self.figure.draw(self.renderer)
File "/media/local_data/languitar/miniconda2/envs/monitoring/lib/python2.7/site-packages/matplotlib/artist.py", line
61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/media/local_data/languitar/miniconda2/envs/monitoring/lib/python2.7/site-packages/matplotlib/figure.py", line
1159, in draw
func(*args)
File "/media/local_data/languitar/miniconda2/envs/monitoring/lib/python2.7/site-packages/matplotlib/artist.py", line
61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/media/local_data/languitar/miniconda2/envs/monitoring/lib/python2.7/site-packages/matplotlib/axes/_base.py",
line 2324, in draw
a.draw(renderer)
File "/media/local_data/languitar/miniconda2/envs/monitoring/lib/python2.7/site-packages/matplotlib/artist.py", line
61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/media/local_data/languitar/miniconda2/envs/monitoring/lib/python2.7/site-packages/matplotlib/axis.py", line 1
106, in draw
ticks_to_draw = self._update_ticks(renderer)
File "/media/local_data/languitar/miniconda2/envs/monitoring/lib/python2.7/site-packages/matplotlib/axis.py", line 1
038, in _update_ticks
if not mtransforms.interval_contains(interval_expanded, loc):
File "/media/local_data/languitar/miniconda2/envs/monitoring/lib/python2.7/site-packages/matplotlib/transforms.py",
line 2780, in interval_contains
((a < b) and (a <= val and b >= val))
TypeError: invalid type promotion
答案 0 :(得分:1)
好的,在将日期转换为matplotlib.dates.date2num
之前,将它们传递给定位器之前就可以了。