2024-04-29 14:15:59 发布
网友
我试图用seaborn线性模型图绘制一个随时间变化的值,但我得到了错误
TypeError: invalid type promotion
我读过,不可能绘制熊猫的日期对象,但这似乎真的很奇怪,因为seaborn要求您将熊猫数据帧传递到绘图中。在
下面是一个简单的例子。有人知道我怎样才能让它工作吗?在
我试着用ggplot做一个我在r中创建的图,所以我想用sns.lmplot公司
我找到了一个从paulh.派生的解决方案,用于在seaborn中绘制时间戳。由于返回了一些后端错误消息,我不得不将它应用到我的数据上。在
在我的解决方案中,我添加了一个matplotlib.ticker函数格式化程序ax.xaxis.set_major_格式化程序. 此functformatter包装了假日期函数。这样,就不需要插入@pyplot.FuncFormatter事先。在
我的解决方案是:
import pandas import seaborn from matplotlib import pyplot, dates from matplotlib.ticker import FuncFormatter 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 }) 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(FuncFormatter(fake_dates)) # legible labels ax.tick_params(labelrotation=45) fig.tight_layout()
我希望这能奏效。在
您需要将日期转换为浮动,然后格式化x轴以重新解释并将浮动设置为日期。在
我可以这样做:
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)
我找到了一个从paulh.派生的解决方案,用于在seaborn中绘制时间戳。由于返回了一些后端错误消息,我不得不将它应用到我的数据上。在
在我的解决方案中,我添加了一个matplotlib.ticker函数格式化程序ax.xaxis.set_major_格式化程序. 此functformatter包装了假日期函数。这样,就不需要插入@pyplot.FuncFormatter事先。在
我的解决方案是:
我希望这能奏效。在
您需要将日期转换为浮动,然后格式化x轴以重新解释并将浮动设置为日期。在
我可以这样做:
相关问题 更多 >
编程相关推荐