Sop*_*Lou 5 python visualization pandas seaborn jupyter-notebook
当我使用seaborn绘制点图时,x轴上的日期变成了这种奇怪的格式2020-01-06T00:00:00.000000000
。在绘制此图形之前,“当前年份周结束”列中的日期将转换为 DateTime 对象。我的其他图表(线图)使用类似的输入和格式,但它没有这个问题,日期就像2020-01-06
.
有谁知道如何解决这个问题?
*我在图中使用的total_us
数据框和数据框是同一数据集的子集。sub
在创建这两个子集之前,我转换了该大数据集的日期。total_us
因此,和的“当前年份周结束”列中的值sub
应具有相同的日期时间格式。
#create a pointplot to capture the variability
plt.figure(figsize = (8, 6))
sns.pointplot(x = 'Current Year Week Ending',
y = 'ASP Current Year',
hue ='Type',
data = sub,
markers=["o", "x"],
linestyles=["-", "--"])
plt.xticks(rotation=45, horizontalalignment='right', fontweight='light', fontsize='medium')
plt.ticklabel_format(style='plain', axis='y')
Run Code Online (Sandbox Code Playgroud)
#create a lineplot
plt.figure(figsize=(10,7))
sns.lineplot(x='Current Year Week Ending',
y='Total Bulk and Bags Units',
hue='Type',
data = total_us);
plt.xticks(rotation=45, horizontalalignment='right', fontweight='light', fontsize='medium')
plt.ticklabel_format(style='plain', axis='y')
Run Code Online (Sandbox Code Playgroud)
这就是sub
看起来的样子,因此“当前年份周结束”列中的值类似于“2020-01-06”。我不知道为什么当我绘制点图时它会改变。
我认为你的日期列可能只包含这些尾随零,而seaborn会根据绘图上有多少空间来不同地处理它。因此,请对日期列尝试以下操作:
sub.iloc[:,1] = sub.iloc[:,1].dt.strftime('Y/%m/%d')
Run Code Online (Sandbox Code Playgroud)
如果由于某种原因不能解决问题(也许seaborn点图和线图在底层做了不同的事情),那么一个可能的解决方法是在2020-01-06T00:00:00.000000000
字母“T”处分割文本,并在后面扔掉尾随零。sns.pointplot
我们可以利用返回一个对象的事实matplotlib.Axes
(来自文档),将变量 ax 设置为等于您的点图:
ax = sns.pointplot(x = 'Current Year Week Ending',
y = 'ASP Current Year',
hue ='Type',
data = sub,
markers=["o", "x"],
linestyles=["-", "--"])
ax.set_xticklabels([date_text.get_text().split("T")[0] for date_text in ax.get_xticklabels()])
Run Code Online (Sandbox Code Playgroud)