seaborn 绘图错误地绘制了 pandas 的 x 轴日期 [英] seaborn plot misplotting x axis dates from pandas

查看:75
本文介绍了seaborn 绘图错误地绘制了 pandas 的 x 轴日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 seaborn 中绘制一个线图,其中 y 轴的值和 x 轴的日期来自熊猫数据框.

I am trying to plot a lineplot in seaborn with values for the y axis and dates for the x axis coming from a pandas dataframe.

当我在不将日期列转换为日期时间对象的情况下创建线图时,它会将日期放在错误的顺序中.当我将日期列转换为日期时间格式时,它会给出奇怪的x标签,并且仅显示5个日期.

When I create the lineplot without converting the date column to datetime object it puts the dates in the wrong order. When I do convert the date column to datetime format it gives strange x labels and only shows 5 of the dates.

df = pd.read_csv("dddd.csv")
df["date"] = pd.to_datetime(df["date"])
df = df.sort_values(["date"])
ax = sns.lineplot(y=df["google"],x=df["date"],color="red",data=df)

plt.show()

我只想绘制数据,其中 x 标签是按顺序排列的日期.这是一些示例数据.

I want to just plot the data with the x labels being the dates have it in order. Here is some example data.

25-03-2019  -100
26-03-2019  -66.66666667
27-03-2019  -80
28-03-2019  -87.08333333
29-03-2019  -88.88888889
30-03-2019  -86.28526646
31-03-2019  -87.5
01-04-2019  -87.87878788
02-04-2019  -82.92682927
03-04-2019  -84.09090909
04-04-2019  -84.7826087
05-04-2019  -85.71428571
06-04-2019  -81.30677848
07-04-2019  -81.98051948
08-04-2019  -82.14285714
09-04-2019  -78.46153846
10-04-2019  -76.05633803
11-04-2019  -75
12-04-2019  -75
13-04-2019  -80
14-04-2019  -83.33333333
15-04-2019  -83.33333333
16-04-2019  -77.77777778
17-04-2019  -68
18-04-2019  -54.70085471
19-04-2019  -64.70588235
20-04-2019  -66.66666667

推荐答案

尽管这个问题肯定是重复的,但这里有一些解释:

Despite this question most certainly being a duplicate, here is some explanation:

  1. 不转换为 datetime,你的 x 轴是无序的,因为 python 不理解这些值是日期.在这里,它可能会为每个点画一个勾号,因为它不知道如何连续适应"16-04-2019".(尝试在 x 轴上绘制一个字符串数组作为比较).

  1. Without converting to datetime, your x-axis is unordered, because python doesn't understand the values are dates. Here it probably draws a tick for every point, because it doesn't know how to fit "16-04-2019" on a continuous scale. (Try plotting an array of strings on the x-axis as comparison).

如果您将 do 转换为 datetime ,则 seaborn 和大熊猫足够聪明,可以注意到您的日期形成了连续的尺度.因为 ticklabels 在绘制所有标签时会重叠,所以它会忽略其中的一些标签,并以固定的间隔绘制标签.

If you do convert to datetime, seaborn and pandas are intelligent enough, to notice your dates form a continuous scale. Because the ticklabels would overlap when drawing all of them, it leaves out some of them and draws ticks in regular intervals instead.

您可以使用 ax.set_ticks(locations) 来影响位置,使用 ax.tick_params(rotation=90, horizo​​ntal_alignment='left') 或它们的标签来影响它们的样式使用 ax.set_xticklabels(labels).(以关键字为例).

You can influence the location with ax.set_ticks(locations), their style with ax.tick_params(rotation=90, horizontal_alignment='left') or their labels with ax.set_xticklabels(labels). (keywords as example).

你也可以使用

import matplotlib.dates ad md

locs = md.YearLocator() # Locations for yearly ticks
format = md.MajorFormatter('%B %Y') # Format for labels

访问格式化函数和 ax.set_major_locator/formatter 以将它们应用于您的绘图.

to access formatter functions and ax.set_major_locator / formatter to apply them to your plot.

要简单地为每个数据点画上一个勾号,在您的情况下,请使用:

To simply draw a tick for every datapoint, in your case use:

ax.set_xticks(df["Date"])

这篇关于seaborn 绘图错误地绘制了 pandas 的 x 轴日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆