擅长:python、mysql、java
<p>您需要将日期转换为浮动,然后格式化x轴以重新解释并将浮动设置为日期。在</p>
<p>我可以这样做:</p>
<pre><code>import pandas
import seaborn
from matplotlib import pyplot, dates
%matplotlib inline
date = ['1975-12-03','2008-08-20', '2011-03-16']
value = [1,4,5]
df = pandas.DataFrame({
'date': pandas.to_datetime(date), # pandas dates
'datenum': dates.datestr2num(date), # maptlotlib dates
'value': value
})
@pyplot.FuncFormatter
def fake_dates(x, pos):
""" Custom formater to turn floats into e.g., 2016-05-08"""
return dates.num2date(x).strftime('%Y-%m-%d')
fig, ax = pyplot.subplots()
# just use regplot if you don't need a FacetGrid
seaborn.regplot('datenum', 'value', data=df, ax=ax)
# here's the magic:
ax.xaxis.set_major_formatter(fake_dates)
# legible labels
ax.tick_params(labelrotation=45)
</code></pre>
<p><a href="https://i.stack.imgur.com/CfUse.png" rel="noreferrer"><img src="https://i.stack.imgur.com/CfUse.png" alt="enter image description here"/></a></p>